Official Soldat Forums

Server Talk => Scripting Releases => Topic started by: KeYDoN on April 07, 2007, 08:45:30 am

Title: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post 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
 
Code: [Select]
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;
Title: Re: [code snippet] function xsplit (Linux compatible)
Post by: nub on April 07, 2007, 03:21:10 pm
Hmm, I always thought GetPiece() wouldn't work because the documentation said, that it's using the regular split() function.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: KeYDoN on April 30, 2007, 08:46:07 pm
LIES all LIES ;)

Date Posted: April 07, 2007, 05:00:44 PM
rewriten for compatibility with serverversion 2.6.0
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: mikembm on April 30, 2007, 10:39:47 pm
Ummm.. this isn't needed anymore is it?
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: EnEsCe on April 30, 2007, 10:53:20 pm
Yes it is needed, split isn't going to be built into the core anymore
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: Avarax on April 30, 2007, 11:08:28 pm
why is that?
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: DorkeyDear on May 12, 2007, 07:54:10 pm
Quote from: mIRC
[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
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: EnEsCe on May 12, 2007, 07:55:54 pm
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.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: deguix on May 14, 2007, 12:30:19 am
weird... then your compatibility compiler sucks ;p
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: chrisgbk on May 14, 2007, 01:36:58 am
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.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: DorkeyDear on May 18, 2007, 09:48:59 pm
I swear! This doesn't work!!!

Quote
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:
Quote
{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
Quote
GetArrayLength(File) - 1
appearnyly arrayhigh wasn't working right  or something :(
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: mikembm on May 18, 2007, 09:52:04 pm
I swear! This doesn't work!!!

Quote
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:
Quote
{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.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: DorkeyDear on May 19, 2007, 12:30:51 pm
Actually, I replaced
Quote
ArrayHigh(File)
with
Quote
GetArrayLength(File) - 1
Strange.... now it works.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: DeMo on May 19, 2007, 08:13:32 pm
Did you declare File as TStringArray?
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: DorkeyDear on May 21, 2007, 09:44:46 pm
Did you declare File as TStringArray?
yup
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: deguix on October 25, 2007, 01:00:24 am
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);".
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: FliesLikeABrick on October 25, 2007, 02:29:33 am
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?
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: urraka on October 25, 2007, 11:16:58 am
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.
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: danmer on January 29, 2008, 12:49:38 pm
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 )
Title: Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
Post by: Kavukamari on January 30, 2008, 07:41:35 pm
any idea why ArrayHigh works only sometimes?

(on the topic of Curt's posts)

by the way, can you xsplit a TStringArray?