Page 1 of 1

Split() and Join()

Posted: Tue Mar 27, 2007 9:04 am
by the.weavster
myArray = Split(myString, myDelimiter)
myString = Join(myArray, myDelimiter)

I can't find these in the help file but I guess they may already exist.

Re: Split() and Join()

Posted: Tue Mar 27, 2007 10:11 am
by PB
For splitting, look up "StringField" in the manual.
For joining, nothing like that yet exists.

Posted: Tue Mar 27, 2007 12:35 pm
by AND51
> For splitting, look up "StringField" in the manual.
Haha, he wants to split arrays, not strings... :wink:

PureBasic is not a scripting language like perl or PHP. Nevertheless, there are very powerful functions in perl, that I would like to see in PB.

Posted: Tue Mar 27, 2007 12:38 pm
by Kaeru Gaman
what should "Join" do, anyhoff?

Code: Select all

a$ = "Bla"
b$ = "Blub"
c$ = " "
d$ = a$ + b$ + c$
or something like that:

Code: Select all

Dim Field.s(9)
;...
Out.s = ""
For n=0 to 9
  Out + Field(n)
Next

Posted: Tue Mar 27, 2007 12:45 pm
by Flype
for those who are interested :

http://www.purebasic.fr/english/viewtopic.php?t=21495


since it is possible in pb4 to pass an array or a linkedlist to a procedure, such features are easy to program.

Posted: Tue Mar 27, 2007 12:53 pm
by PB
> Haha, he wants to split arrays, not strings

What's so funny? His requested syntax is...

myArray = Split(myString, myDelimiter)

...which looks like he wants to create an array ("MyArray") from "MyString".
So if "MyString" was "a/b/c/d/e" and the delimiter was "/" then his requested
command would create an array of MyArray(1)="a", MyArray(2)="b", etc.

Posted: Tue Mar 27, 2007 2:16 pm
by Trond

Code: Select all

Procedure Split(S.s, Delimiter.s, Array.s(1))
  Protected I
  Protected C = CountString(S, Delimiter)
  For I = 0 To C
    Array(I) = StringField(S, I+1, Delimiter)
  Next
  ProcedureReturn C
EndProcedure

Procedure.s Join(Delimiter.s, Count.l, Array.s(1))
  If Count
    ProcedureReturn Join(Delimiter, Count-1, Array()) + Delimiter + Array(Count)
  Else
    ProcedureReturn Array(0)
  EndIf
EndProcedure

Dim MyArray.s(10)
Test.s = "Jackdaws love my big sphinx of quartz."
C = Split(Test, " ", MyArray())
For I = 0 To C
  Debug MyArray(I)
Next

Debug "-----"

Debug Join(" ", C, MyArray())

Posted: Tue Mar 27, 2007 2:22 pm
by AND51
Yes PB, you're right.

Posted: Wed Mar 28, 2007 4:44 pm
by the.weavster
Is looping the only way?

In REALbasic the equivalent to StringField() is NthField(). If you use NthField() and a loop to create your own array from a large block of delimited text it can take an inordinate amount of time to complete, maybe even make the program stop responding.

However Split() does the job in the blink of an eye.

I thought maybe there was something cleverer going on.

Posted: Wed Mar 28, 2007 5:46 pm
by Trond
That's because RealBasic is extremely slow compared to PureBasic. Split() and Join() doesn't do anything else than looping internally either.

Posted: Wed Mar 28, 2007 6:04 pm
by freak
Using StringField() is still not the fastest way, as StringField always has to look at the string from the start with each call.
If you examine the string via pointers, you can get each field without ever looking at allready processed parts.