SRL Archive - SRL Archives

Explode() Function


Ron

2006-11-01 19:34:58

Explode() Function


The explode function, that I made which was fixed by Weibs, will make an array very easily, just by doing this.

SomeArray := Explode('*', 'happy*sad*mad*angry*excited*bored');

It will then make an array with values of happy for SomeArray[0], sad for SomeArray[1], mad for SomeArray[2], etc. This is very useful if you wanted a person to provide a list of colors, and you made the separator a comma. Then in the const, you can have something like…


Const
Colors := '034294234,18473981,23423974,2384792384,23842397';

and then you could make an explode call down below like this.


ColorArray := Explode(',', Colors);

and it will do set each value to the ColorArray. :D

Explode() function code only:

// The Explode procedure looks through a string and separates it with whatever
// you suplied as a separator, and takes each value and puts them into an array.
// Example: Explode('*', 'a*b*c*d*e*');
// Made by Ron :)
function Explode(Separator, Str : string) : array of string;
var
i : integer;
begin
if(pos(Separator, Str) > 0)then
repeat
SetArrayLength(Result, i + 2);
Result[i] := copy(Str, 1, pos(Separator, Str)-1);
delete(Str, 1, pos(Result[i], Str)+Length(Result[i]));
i := i + 1;
until(pos(Separator, Str) = 0)
else
SetArrayLength(Result, 1);
Result[i] := Str;
end;

Test the Explode() function by using this program.

program TestExplode;

var
MyArray : Array of String;
i : Integer;

// The Explode procedure looks through a string and separates it with whatever
// you suplied as a separator, and takes each value and puts them into an array.
// Example: Explode('*', 'a*b*c*d*e*');
// Made by Ron :)
function Explode(Separator, Str : string) : array of string;
var
i : integer;
begin
if(pos(Separator, Str) > 0)then
repeat
SetArrayLength(Result, i + 2);
Result[i] := copy(Str, 1, pos(Separator, Str)-1);
delete(Str, 1, pos(Result[i], Str)+Length(Result[i]));
i := i + 1;
until(pos(Separator, Str) = 0)
else
SetArrayLength(Result, 1);
Result[i] := Str;
end;

begin
ClearDebug;
MyArray := Explode('*', 'ass*bitch*cunt*doosh*erm*fuck');
for i := 0 to GetArrayLength(MyArray) - 1 do
WriteLn(' MyArray[' + IntToStr(i) + '] := ''' + MyArray[i] + ''';');
end.

I hope you liked it!

~Ron :spot:

Sdcit

2006-11-01 19:40:28

Wait, so did you make this?

Ron

2006-11-01 20:17:29

uhh yeah… All the code was made by me, and it was fixed by another coder, Weibs. :)

All my functions/procedures are in these two links. I just like this one the best, so I only posted this.

Ron's Functions & Procedures #1 - Contains 7
Ron's Functions & Procedures #2 - Contains 4

~Ron

Boreas

2006-11-01 21:10:27

Very nice Ron. I'm using it for ISM commands, giving you credit of course.

Yakman

2006-11-02 11:23:06

i think Mopar, already made something like this, include ArrayLoader.scar its in SRL\Misc

the function is function strToStrArray(strArray: String): Array of String;

the main differance is you cant choose your separator, it has to be a space

lardmaster

2006-11-02 11:25:18

thank you so much! this will be emmensley usefull for my extremely long list in my tut runner!

Ron

2006-11-02 14:36:44

From: Yakman
i think Mopar, already made something like this, include ArrayLoader.scar its in SRL\Misc

the function is function strToStrArray(strArray: String): Array of String;

the main differance is you cant choose your separator, it has to be a space


Hmm, I never even saw Mopar's.

I translated the PHP function Explode() into scar code, and I made this from scratch, so in case anyone is wondering, I never ripped Mopar. ;)

And you're welcome lardmaster and boreas. I used this in a number of my scripts as well. It's very useful!

Edit: I edited the first post because the code indentation and lines were a little messed up because I used those stupid scar tags. Apparently the scar tags don't work as well as they do on Mopar's forum, so I changed the last two code samples from scar tags to code tags, and it fixed the problem.