Author Topic: [code snippet] function xsplit (Linux compatible) *UPDATE*  (Read 9418 times)

0 Members and 1 Guest are viewing this topic.

Offline KeYDoN

  • Major
  • *
  • Posts: 60
[code snippet] function xsplit (Linux compatible) *UPDATE*
« 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;
« Last Edit: April 30, 2007, 08:49:24 pm by KeYDoN »

Offline nub

  • Major
  • *
  • Posts: 56
Re: [code snippet] function xsplit (Linux compatible)
« Reply #1 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.
Можно зарезать, украсть и всё-таки быть счастливым...

Offline KeYDoN

  • Major
  • *
  • Posts: 60
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #2 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

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #3 on: April 30, 2007, 10:39:47 pm »
Ummm.. this isn't needed anymore is it?

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #4 on: April 30, 2007, 10:53:20 pm »
Yes it is needed, split isn't going to be built into the core anymore

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #5 on: April 30, 2007, 11:08:28 pm »
why is that?
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #6 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

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #7 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.

Offline deguix

  • Major
  • *
  • Posts: 79
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #8 on: May 14, 2007, 12:30:19 am »
weird... then your compatibility compiler sucks ;p
« Last Edit: May 14, 2007, 12:33:10 am by deguix »

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #9 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.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #10 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 :(
« Last Edit: May 19, 2007, 12:31:37 pm by DorkeyDear »

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #11 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.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #12 on: May 19, 2007, 12:30:51 pm »
Actually, I replaced
Quote
ArrayHigh(File)
with
Quote
GetArrayLength(File) - 1
Strange.... now it works.

Offline DeMo

  • Soldier
  • **
  • Posts: 127
  • Stay Metal! \m/
    • Encoder 2002
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #13 on: May 19, 2007, 08:13:32 pm »
Did you declare File as TStringArray?

<@Evil-Ville> Expect a picture of Chakra` holding his fleshlight soon!

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #14 on: May 21, 2007, 09:44:46 pm »
Did you declare File as TStringArray?
yup

Offline deguix

  • Major
  • *
  • Posts: 79
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #15 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);".
« Last Edit: October 25, 2007, 01:51:11 am by deguix »

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #16 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?

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #17 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.
urraka

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #18 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 )

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: [code snippet] function xsplit (Linux compatible) *UPDATE*
« Reply #19 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?
« Last Edit: January 30, 2008, 07:46:05 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."