join / array to string

Started by vito, Jul 04, 2016, 06:47 PM

Previous topic - Next topic

vito

I noticed no join method for arrays in squirrel so I made it:
function join_array(arr,sep){
local i = 0;local l = arr.len();local r = "";
if(l == 0){return "";} else if(l == 1){return arr[0].tostring();} else {r=arr[0].tostring();i=1;while(i < arr.len()){r = r + sep + arr[i].tostring();i++;}return r;};
}

Using
"hello, world, true" == join_array(["hello", "world", true], ", ")

.

Christ, what a horrible coding style you have. Why do you have to make everything in one line when it looks so ugly.

function join_array(arr, sep)
{
    if (typeof(arr) != "array" || arr.len() <= 0)
    {
        return "";
    }

    local str = "", sep = sep.tostring();

    foreach (val in arr)
    {
        str += val;
        str += sep;
    }

    return str.slice(0, -sep.len());
}
.

Thijn

Im wondering, is this:
str += val + sep;

faster / nicer then this:

str += val;
str += sep;

.

Not really since (val + sep) will be created as a separate string and deleted immediately after added to (str). Where as the other method simply adds 2 strings to (str) without making a temporary string. But it never hurts to benchmark that :D
.

KAKAN

this was released before, in the old forum, if I remember it :p
oh no

Stormeus

An improvement, if I may. This can be done in one line without error checking.

function join_array(arr, sep) {
    if (typeof(arr) != "array") {
        throw "join_array expected array input, got " + typeof(arr) + " (" + arr + ")";
    }
    else if (typeof(sep) != "string") {
        throw "join_array expected string separator, got " + typeof(sep) + " (" + sep + ")";
    }
    else if (arr.len() <= 0) {
        return "";
    }

    return arr.reduce(@(a, b) a + sep + b);
}

function test_join_array(arr, sep) {
    try {
        print("> " + join_array(arr, sep));
    }
    catch (e) {
        print("! Error: " + e);
    }
}

test_join_array(["test", "hello", "world"], ", ");
test_join_array(["single"], " | ");
test_join_array("string", ";");
test_join_array(["my", "array"], 7);
test_join_array([], "");

Quote from: console output[SCRIPT] > test, hello, world
[SCRIPT] > single
[SCRIPT] ! Error: join_array expected array input, got string (string)
[SCRIPT] ! Error: join_array expected string separator, got integer (7)
[SCRIPT] >

ysc3839

Quote from: Stormeus on Jul 05, 2016, 05:44 PMAn improvement, if I may. This can be done in one line without error checking.

function join_array(arr, sep) {
    if (typeof(arr) != "array") {
        throw "join_array expected array input, got " + typeof(arr) + " (" + arr + ")";
    }
    else if (typeof(sep) != "string") {
        throw "join_array expected string separator, got " + typeof(sep) + " (" + sep + ")";
    }
    else if (arr.len() <= 0) {
        return "";
    }

    return arr.reduce(@(a, b) a + sep + b);
}

function test_join_array(arr, sep) {
    try {
        print("> " + join_array(arr, sep));
    }
    catch (e) {
        print("! Error: " + e);
    }
}

test_join_array(["test", "hello", "world"], ", ");
test_join_array(["single"], " | ");
test_join_array("string", ";");
test_join_array(["my", "array"], 7);
test_join_array([], "");

Quote from: console output[SCRIPT] > test, hello, world
[SCRIPT] > single
[SCRIPT] ! Error: join_array expected array input, got string (string)
[SCRIPT] ! Error: join_array expected string separator, got integer (7)
[SCRIPT] >
I know reduce in Python. I can't believe that Squirrel also have it.

Stormeus


vito

Quote from: KAKAN on Jul 05, 2016, 12:58 PMthis was released before, in the old forum, if I remember it :p

now it's here xD