It is currently Mon Sep 23, 2019 2:35 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [DONE] Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 5:39 pm 
Offline
User
User

Joined: Sat Jan 19, 2008 9:30 am
Posts: 33
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!

_________________
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650


Last edited by PresFox on Sat Apr 03, 2010 6:40 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 5:48 pm 
Offline
Always Here
Always Here
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 5755
Location: Berlin - Germany
http://www.purebasic.com/documentation/ ... field.html

greetings
Thomas


Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 6:00 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3573
Location: Utah, USA
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:
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:

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 6:05 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2437
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 6:40 pm 
Offline
User
User

Joined: Sat Jan 19, 2008 9:30 am
Posts: 33
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 6:43 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3573
Location: Utah, USA
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

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Explode? Some way to split a string into an array?
PostPosted: Sat Apr 03, 2010 10:46 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Mar 16, 2008 9:18 am
Posts: 469
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: [DONE] Some way to split a string into an array?
PostPosted: Fri Dec 30, 2016 11:05 pm 
Offline
User
User

Joined: Fri Mar 25, 2016 2:02 pm
Posts: 40
Location: Europe
Sorry for bringing the life to this topic, in the future others may need the same function.
There is my more "advanced" version:

Code:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye