Split() and Join()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Split() and Join()

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Split() and Join()

Post by PB »

For splitting, look up "StringField" in the manual.
For joining, nothing like that yet exists.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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
oh... and have a nice day.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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())
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Yes PB, you're right.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

That's because RealBasic is extremely slow compared to PureBasic. Split() and Join() doesn't do anything else than looping internally either.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
Post Reply