SRL Archive - Research & Development Lounge

Explode


A G E N T

2007-12-11 19:40:03

Sorry if you feel cheated, but no explosions in this thread. Aww what the hell. Have one. http://sightsonics.cf.huffingtonpost.com/nuclear-explosion.jpg

Anyway, that stupidity out of the way :duh:

I got bored and made a function, explode. If you've ever used PHP, Perl, or the Java equivalent, Split(), then you know what it is. If not, it takes a string and splits it into an array, based on a common delimiter.

ex: "Cheese+Is+Tasty" Would become Arr[0] := 'Cheese', Arr[1] := 'Is' Arr[3] := 'Tasty'

To be honest, I can't even think where it would be useful, but…what the hay. I made it anyway. Here it is:

program Explosion;

//Ohai
//Why did I make this? I dunno. I got bored.

var
TestArr : TStringArray;
i2 : integer;

function Explode(Delimiter : string; Str : string) : array of string;
var
DelimPos, i : integer;
Arr1 : array of string;
begin
SetArrayLength(Arr1, 100000);
i := 0;
repeat
if (pos(Delimiter, Str) = 0) then Break;
DelimPos := pos(Delimiter, Str);
Arr1[i] := Replace((copy(Str,0,DelimPos)),Delimiter,'');
Delete(Str,1,DelimPos);
i := i + 1;
until(pos(Delimiter, Str) = 0);
Arr1[i] := Str;
SetArrayLength(Arr1, i+1);
Result := Arr1;
end;



//Do whatever you want with it.
begin
TestArr := Explode('-','Runescape-Is-Fun-But-Pking-Sucks');
i2 := 0;
repeat
Writeln('Array['+IntToStr(i2)+'] = '+TestArr[i2]);
i2 := i2 + 1;
until(i2 = GetArrayLength(TestArr));
end.

MylesMadness

2007-12-12 15:49:21

I might use this to store autoing talking sentences.

munk

2007-12-12 16:02:29

nice! explode is one of my favorite PHP functions but i never really saw a use for it on SCAR. looks real good though.

A G E N T

2007-12-12 16:25:22

Thanks for the nice feedback guys :)

Boreas

2007-12-12 23:25:28

About a year ago I had a port phasmatys smelter that used 2 SCARs with ISM, and it would send lots of info in 1 string, and then separate with explode. Quite useful.

Bobarkinator

2007-12-13 01:58:19

procedure Split(Text: string; Delimiter: Char; var Dest: TStringList);
begin
Dest.Clear;
Dest.DelimitedText := Text;
Dest.Delimiter := Delimiter;
end;

Test folder of the scar directory

Used like

program TStringListExample2;
var
strlist: TStringList;
i: Integer;

procedure Split(Text: string; Delimiter: Char; var Dest: TStringList);
begin
Dest.Clear;
Dest.DelimitedText := Text;
Dest.Delimiter := Delimiter;
end;

begin
strlist := TStringList.Create;
Split('45d8,578,4,726,keo', ',', strlist);
for i := 0 to strlist.Count - 1 do
WriteLn(strlist.Strings[i]);
strlist.Free;
end.

n3ss3s

2007-12-13 07:57:56

Bob whats the difference between TStringArray and TStringList except for that StringList is a record type?

Bobarkinator

2007-12-13 10:10:55

PM Freddy. I have no idea

Markus

2007-12-13 10:21:36

TStringArray is an array, TStringList has manipulation functions and stuff like that (add, remove, loadfromfile etc).

n3ss3s

2007-12-13 12:08:02

Oh, okay, thanks markus.