[DONE] Some way to split a string into an array?

Just starting out? Need help? Post your questions and find answers here.
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

[DONE] Some way to split a string into an array?

Post by PresFox »

Hello,

Next question (im making seperate threads to increase searchability for others, please let me know if this is frowned upon)

I have a string wich consists of several values, split by a delimiter, eg: ABC*DEFG*JKL

I want to split this in 3 different values, ABC, DEFG, and JKL.

In PHP ( wich i do know), i could explode this to an array, but how would i do this in PB?

Is using FindString and MID the only way? or is there a explode like function?

Thanks in advance!
Last edited by PresFox on Sat Apr 03, 2010 6:40 pm, edited 1 time in total.
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Explode? Some way to split a string into an array?

Post by ts-soft »

User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Explode? Some way to split a string into an array?

Post by Demivec »

PresFox wrote:Is using FindString and MID the only way? or is there a explode like function?
CountString() and StringField() should do the trick.

Code: Select all

Procedure explodeStringArray(Array a$(1), s$, delimeter$)
  Protected count, i
  count = CountString(s$,delimeter$) + 1
  
  Debug Str(count) + " substrings found"
  Dim a$(count)
  For i = 1 To count
    a$(i - 1) = StringField(s$,i,delimeter$)
  Next
  ProcedureReturn count ;return count of substrings
EndProcedure

Dim output.s(0) ;this will be resized later

explodeStringArray(output(), "ABC*DEFG*JKL", "*")
For i = 0 To ArraySize(output())
  Debug output(i)
Next
I likewise second ts-soft's suggestion to peruse the manual. :wink:
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Explode? Some way to split a string into an array?

Post by Michael Vogel »

Not sure, if StringField is the wanted "solution", it works fine and simple but costs execution time for each call.

Let's say, PresFox wants to load a textfile and do a lot of line per line manipulations within this text - then an "explode" would be nice, if the single string would be transformed to an array (but only, if an implode would also exist :wink:)

Michael

[EDIT] Demivec's code demonstrate how such an explode could work - the only thing to be aware is that (large) strings are kept two times in the memory here
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

Re: Explode? Some way to split a string into an array?

Post by PresFox »

Demenivec, thanks, works wonders! :)
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Explode? Some way to split a string into an array?

Post by Demivec »

PresFox wrote:Demenivec, thanks, works wonders! :)
Your welcome. It is only one of the ways (and not necessarily the best way) to accomplish the task. :D
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Explode? Some way to split a string into an array?

Post by UserOfPure »

Another variation, which takes into account multiple asterisks and doesn't include them. (Demivec forgot to check that, try his code with "*ABC*****DEFG***JKL*"). ;)

Code: Select all

Dim a$(100)

s$="*ABC*****DEFG***JKL*"

Repeat
  f=FindString(s$,"*",1)
  If f
    t$=Left(s$,f-1)
    If t$<>"" : c+1 : a$(c)=t$ : EndIf
    s$=Mid(s$,f+1)
  EndIf
Until f=0
If s$<>"" : c+1 : a$(c)=s$ : EndIf

Debug Str(c)+" substrings found"
For a=1 To c
  Debug a$(a)
Next
xakep
User
User
Posts: 40
Joined: Fri Mar 25, 2016 2:02 pm
Location: Europe

Re: [DONE] Some way to split a string into an array?

Post by xakep »

Sorry for bringing the life to this topic, in the future others may need the same function.
There is my more "advanced" version:

Code: Select all

Procedure.l GetArray_Between(String.s, Left.s, Right.s, Array InArray.s(1))
  Define LenString.l, LeftMarker.l, RightMarker.l, NewString.s, LeftL.l, iCount.l, MaxLoops.l, cLoop.l
  
  MaxLoops = 99 ;Just to be safe
  
  Repeat
    cLoop + 1
    
    If NewString
      String = NewString
    EndIf
    
    LenString = Len(String)
    LeftL = Len(Left)
    
    If LenString And LeftL
    
      LeftMarker = FindString(String, Left)
    
      If LeftMarker>0
      
        RightMarker = FindString(String, Right, LeftMarker+1)
      
        If RightMarker>0
        
          InArray(iCount) = Mid(String, LeftMarker+LeftL, (RightMarker-LeftMarker)-LeftL)
             
          NewString = Right(String, LenString - RightMarker)
          
          If Len(NewString) < (LeftL * 2) ;To be extra safe =)
            NewString = ""
          Else
            iCount + 1
            ReDim InArray(iCount)
          EndIf

          Else
           FreeArray(InArray())
        EndIf
      Else 
        FreeArray(InArray())
      EndIf
    Else
      FreeArray(InArray())
    EndIf

  Until NewString = "" Or cLoop => MaxLoops 
 
EndProcedure
 
 ;Test
 
Define MyStr.s, i.i

;MyStr = "(a)(b)(c)"
;MyStr = "[[a]][[b]][[c]]"
;MyStr = "{*a1*}{*a2*}{*a3*}{*a79*}"
MyStr = "$#a11}$#b2345}$#c157}$#d this works too}"

Dim MyOutput.s(0)
GetArray_Between( MyStr, "$#", "}", MyOutput())

For i = 0 To ArraySize(MyOutput())
    Debug MyOutput(i)
Next
Post Reply