Official Soldat Forums
Server Talk => Scripting Releases => Topic started by: KeYDoN on April 07, 2007, 08:45:30 am
-
Script Name: xsplit
Script Description: This function will split a string into an array of strings, with (delimiter) as the splitting point. (Linux compatible)
Original Author(s): KeYDoN
Core Version: 1.0.0 (sv2.6.0)
Now compatible to Server 2.6.0 :D
function xsplit(const source: string; const delimiter: string):TStringArray;
var
i,x,d:integer;
s:string;
begin
d:=length(delimiter);
x:=0;
i:=1;
SetArrayLength(Result,1);
while(i<=length(source)) do begin
s:=Copy(source,i,d);
if(s=delimiter) then begin
inc(i,d);
inc(x,1);
SetArrayLength(result,x+1);
end else begin
result[x]:= result[x]+Copy(s,1,1);
inc(i,1);
end;
end;
end;
-
Hmm, I always thought GetPiece() wouldn't work because the documentation said, that it's using the regular split() function.
-
LIES all LIES ;)
Date Posted: April 07, 2007, 05:00:44 PM
rewriten for compatibility with serverversion 2.6.0
-
Ummm.. this isn't needed anymore is it?
-
Yes it is needed, split isn't going to be built into the core anymore
-
why is that?
-
[19:52:22] <DorkeyDear> Hello.
[19:52:53] <DorkeyDear> I have a question... I downloaded the 2.6.1 dedicated server pre-release, and was wondering whats up with the Split function.
[20:04:54] <EnEsCe> split is being removed
[20:05:01] <EnEsCe> problems with TStringArray
[20:05:07] <EnEsCe> can still use xsplit and getpiece
-
People are gonna have to use xsplit, I can not include Split as built-in now because the TStringArray variable causes access violations when being used between soldatserver<->script.
Is more specific.
-
weird... then your compatibility compiler sucks ;p
-
It's not a problem with the compiler; it's due to the script engine. On windows, the only reason it works is by chance, and that windows is more forgiving when it comes to memory. Linux, on the other hand, segfaults because it is more stringent.
-
I swear! This doesn't work!!!
File := XSplit(ReadFile('CashSpot.txt'),chr(13) + chr(10));
for i := 0 to ArrayHigh(File) do WriteConsole(ID,InttoStr(i) + '/' + InttoStr(ArrayHigh(File)) + ': ' + File,$FF33FFAA);
where as the file is:
{SS} Curtle the TurtleÆ400,100
{SS} Curtle the TurtleÆ400,101
{SS} Curtle the TurtleÆ400,102
{SS} Curtle the TurtleÆ400,103
{SS} Curtle the TurtleÆ1,2
and File[0] is the only outcome and it contains the whole file... >:(
and yes, it is on a windows server...
EDIT: fixed... replaced it with GetArrayLength(File) - 1
appearnyly arrayhigh wasn't working right or something :(
-
I swear! This doesn't work!!!
File := XSplit(ReadFile('CashSpot.txt'),chr(13) + chr(10));
for i := 0 to ArrayHigh(File) do WriteConsole(ID,InttoStr(i) + '/' + InttoStr(ArrayHigh(File)) + ': ' + File,$FF33FFAA);
where as the file is:
{SS} Curtle the TurtleÆ400,100
{SS} Curtle the TurtleÆ400,101
{SS} Curtle the TurtleÆ400,102
{SS} Curtle the TurtleÆ400,103
{SS} Curtle the TurtleÆ1,2
and File[0] is the only outcome and it contains the whole file... >:(
and yes, it is on a windows server...
I think it's because you are splitting at both chr(13) and char(10)
It's probably just going to be one of them that you need to split at.
-
Actually, I replaced
ArrayHigh(File)
with GetArrayLength(File) - 1
Strange.... now it works.
-
Did you declare File as TStringArray?
-
Did you declare File as TStringArray?
yup
-
OK... I found an interesting bug:
If I put the xsplit inside a "for" statement and call the function really fast, the resulting variable from the xsplit function is not cleared of items (might be really specific for TStringArray), thus the first item of the resulting list would begin with the string gotten from the previous call of xsplit. Thus, there needs to be a line "SetArrayLength(Result,0);" right before "SetArrayLength(Result,1);".
-
OK... I found an interesting bug:
If I put the xsplit inside a "for" statement and call the function really fast, the resulting variable from the xsplit function is not cleared of items (might be really specific for TStringArray), thus the first item of the resulting list would begin with the string gotten from the previous call of xsplit. Thus, there needs to be a line "SetArrayLength(Result,0);" right before "SetArrayLength(Result,1);".
can you show us the code snippet you did this with?
-
I remember having a similar problem when assigning the result of xsplit to an array which i had used before. I had to call SetArrayLength(array, 0) before doing the assignment. Anyway, I don't think that's related to the variable "Result" inside the function.
-
can you show us the code snippet you did this with?
like here: http://www.nopaste.com/p/avZLLmVrZ
the result: http://www.nopaste.com/p/a61pWVUiV (notice the slashes)
Uncommenting any of the two commented lines helps (like deguix already mentioned). I guess that's just the way TStringArray works ::) (it's not even documented and maybe i shouldn't even whine because of this and just use it as it is :P )
-
any idea why ArrayHigh works only sometimes?
(on the topic of Curt's posts)
by the way, can you xsplit a TStringArray?