SRL Archive - Research & Development Lounge

Function StrToArray


mixster

2008-01-18 14:32:13

After playing with php and scar a bit more, I realised that (esecially) when using databases to create pages for scar to then interpret, it became pretty hard to be able to get those values into scar, thus StrToArray was made.


Function StrToArray(Var stra: array of String; str,del: String): Boolean;
Var
sx,i: Integer;

Begin
If(Not(Right(str,Length(del)) = del)) Then
str:= str+del;
Repeat
Begin
sx:= Pos(del,str);
SetArrayLength(stra,GetArrayLength(stra)+1);
stra[i]:= Copy(str,1,sx-1);
Delete(str,1,sx+(Length(del)-1));
i:=i+1;
End
Until(str = '')
Result:=True;
End;
It works by finding where the first delmiter is and copying from the start until it gets to the delimiter and puts it into the array. It then deletes the part it has copied and the delimiter before looping again until the string (str) is empty. It makes the array's length 1 larger everytime it loops so there's no out of range errors.

You would then use it as

StrToArray(stringArray,String,Delimiter);
or

StrToArray(Scripts,'hello. moo. carrot','. ');

With (in the 1st) stringArray being the array it puts all the values in, String being the string it takes the text from before converting it and Delimiter being the characters that separate each different piece of text (normally ', ' or ',').

In the 2nd case, it would assign 'hello' to stringArray[0], 'moo' to stringArray[1] and 'carrot' to stringArray[2].

Feel free to post improvements/questions or anything else.

Naum

2008-01-18 16:25:49

Nice peice. Might use soon
GJ!!!

Negaal

2008-01-18 16:50:41

Dude…you should try makeing runescape script and apply for members:)

Nice one btw:D

mixster

2008-01-18 17:02:32

I would make a runscape script, but it's just too much work (well, for me atleast, as I am a perfectionist when it comes to most of my scripting). Anyway, currently just working on a few bits and bobs and this was core to being able to get a list of text out of the database and into an array (which is mainly what I'm playing with) and it seemed like it could be useful to a few people.

nielsie95

2008-01-18 17:14:05

Nice =]

Have you also looked at TStringListExample2 from SCAR? It's in your scripts/Test/ folder.


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.

Not a TStringArray, but still might be usefull :)

King of Knives

2008-01-18 17:21:27

It seems like a good start to know how SCAR works with things. The more perfectionist you are with your scripts, the better they will be. You should really try it.

-Knives