vito

  • Sr. Member
  • Posts: 405
join / array to string
« on July 4th, 2016, 07:47 PM »Last edited on July 4th, 2016, 07:49 PM
I noticed no join method for arrays in squirrel so I made it:
Code: [Select]
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
Code: [Select]
"hello, world, true" == join_array(["hello", "world", true], ", ")

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: join / array to string
« Reply #1, on July 4th, 2016, 08:19 PM »
Christ, what a horrible coding style you have. Why do you have to make everything in one line when it looks so ugly.

Code: [Select]
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

  • VC:MP Beta Tester
  • Posts: 1,688

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: join / array to string
« Reply #3, on July 4th, 2016, 09:32 PM »
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

  • Wiki Contributor
  • Posts: 3,343
Re: join / array to string
« Reply #4, on July 5th, 2016, 01:58 PM »
this was released before, in the old forum, if I remember it :p
oh no

Stormeus

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,127
Re: join / array to string
« Reply #5, on July 5th, 2016, 06:44 PM »
An improvement, if I may. This can be done in one line without error checking.

Code: [Select]
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

  • VC:MP Beta Tester
  • Posts: 622
Re: join / array to string
« Reply #6, on July 5th, 2016, 07:27 PM »
Quote from Stormeus on July 5th, 2016, 06:44 PM
An improvement, if I may. This can be done in one line without error checking.

Code: [Select]
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

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,127

vito

  • Sr. Member
  • Posts: 405