GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2014-08-24 09:24:37

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

explode_string Studio Update

Expand/// explode_string(sep,data)
/*
**  Usage:
**      var arr = explode_string(sep,data);
**
**  Arguments:
**      sep         separator character, string
**      data        array data, string
**
**  Returns:
**      array where entry 0 array[0] is the number of items read and array[1....n] is the data as strings
**
**  Notes:
**      Converts a string of data with elements separated
**      by a delimiter into an array of strings.
**
**  GMLscripts.com, modified by icuurd12b42
*/
{
    var arr,sep,dat,len,ind,pos;
    arr[0] = 0;
    sep = argument0;
    dat = argument1 + sep;
    len = string_length(sep);
    ind = 1;
    repeat (string_count(sep,dat)) {
        pos = string_pos(sep,dat)-1;
        arr[ind] = string_copy(dat,1,pos);
        dat = string_delete(dat,1,pos+len);
        ind += 1;
        arr[0] +=1;
    }
    return arr;
}

Last edited by icuurd12b42 (2014-08-24 09:25:33)

Offline

#2 2014-08-25 00:30:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: explode_string Studio Update

GM:Studio can handle arrays gracefully now, which is wonderful. I always hated having to use the old array accessors like variable_local_array_set().

This along with most of the other scripts on the site have been updated for GM:Studio, although they have not all been tested or posted. Thanks for posting this in the meantime.

Something I do differently is not storing the length of the array as the first element. I don't see the need when we have array_length_1d(array). Maybe I'm not understanding something? Is it there because the old version explicitly returns the length?


Abusing forum power since 1986.

Offline

#3 2014-08-31 13:18:45

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: explode_string Studio Update

I actually use the first array element from my experience with arrays used to return varying amounts of data, it is quite a common practice. I considered the array_get_length option but decided against it for ease of use and to support instances when nothing is found in the string, (what to return is there is no entries found??)...

1-You always get an array even when 0 values are found so
2-you don't need to check the return with if(is_array());
3-so that to make sure array_get_length_1d() does not blow up in your face

var t = explode_string(",","1,2,3,4,5");
i =1;
repeat(t[0]) do_stuff(t[i++]);

vs
var t = explode_string(",","1,2,3,4,5");
if(is_array(t))
{
i =0;
ct = array_length_1d(t);
repeat(ct) do_stuff(t[i++]);
}

Last edited by icuurd12b42 (2014-08-31 13:30:37)

Offline

#4 2015-01-31 05:36:10

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: explode_string Studio Update

OK, after five months of continuous thought, I've decided not to use this alteration.

I can see the obvious utility of it. You make a persuasive case.

But I just have problems with it on semantic grounds. It's like you've got this array of elements and then this thing that isn't an element tacked to it. I have a hard time accepting that.

The other problem is it duplicates state information (the length of the array), which is always a bad idea. Not dangerous in this specific case where you know what to expect, but in general it is not a good engineering pattern.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB