Why No Split(String To Array)?

Just starting out? Need help? Post your questions and find answers here.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Why No Split(String To Array)?

Post by TheAutomator »

StringField() is slow and tedious if you have to use it more than once.
Every time it gets called it wil go over the entire string again and again..

I have problems understanding why PB has a StringField() function but no Split() -> array function like in most programming languages..

With a "split/explode to array" function it's easy and efficient to manipulate text files,
for example when you need to divide text files to separate lines by splitting it by CRLF.

I'm working on a project at the moment and I need to separate words and text files strings by " " or newline characters.
Are there any good alternatives for the SpringField function that are not to complicated?

If anyone would like to share some code better than mine (or helpful corrections) that would be nice :)

Here is what I came up with but I'm not sure this is any faster/better:

Code: Select all

Procedure Split(inp.s, del.s, Array arr.s(1))
	len = Len(inp)
	dln = Len(del)
	sta = 1
	For cur = 1 To len
		If Mid(inp, cur, dln) = del
			;Debug Mid(inp, sta, cur - sta)
			ReDim arr(ArraySize(arr()) + 1)
			arr(ArraySize(arr()) - 1) = Mid(inp, sta, cur - sta)
			sta = cur + dln
		EndIf
	Next
	;Debug Mid(inp, sta, cur - sta)
	ReDim arr(ArraySize(arr()) + 1)
	arr(ArraySize(arr()) - 1) = Mid(inp, sta, cur - sta)
EndProcedure

Dim arr_split.s(0)
Split("split,me,please", "0", arr_split())

For x = 0 To ArraySize(arr_split()) - 1
	Debug arr_split(x)
Next
User avatar
idle
Always Here
Always Here
Posts: 5872
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why No Split(String To Array)?

Post by idle »

try this it fills out a list but it can also be adapted to do an array but a list is probably quicker than redim

it assumes a null terminated string and anything below the separator is considered a separator

Code: Select all

Procedure SplitStringList(*source,List StringFields.s(),separator=' ') 
  Protected *inp.Character    
  ClearList(StringFields()) 
  
  If *source 
    *inp = *source 
     While *inp\c <> 0 
      While (*inp\c > separator )   
        *inp+2 
      Wend 
      AddElement(StringFields()) 
      StringFields()= PeekS(*source,(*inp-*source)>>1)
      If *inp\c <> 0 
        While *inp\c <= separator 
        *inp+2 
        *source = *inp 
        Wend  
      Else 
        Break 
      EndIf   
    Wend 
  EndIf 
    
EndProcedure 

Define s.s = "This is	a test	   	 	 	 	 	string	   	 	 	 	 	to	   " + #CRLF$ +  " 	see	   	 	 	 	 	If	split  are	working."

NewList strings.s() 

SplitStringList(@S,Strings()) 

ForEach strings() 
  Debug Strings()
Next   

User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Why No Split(String To Array)?

Post by TheAutomator »

This one groups the spaces if i'm right?
I would prefer it splitting every time a delimiter is encountered personally but I can alter it :)
Also, it can not work with delimiters like "|-|" for example right?

My next idea was using a regular expression maybe, not sure how fast those are..

Thanks for your reply :)
User avatar
idle
Always Here
Always Here
Posts: 5872
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why No Split(String To Array)?

Post by idle »

AZJIO
Addict
Addict
Posts: 2162
Joined: Sun May 14, 2017 1:48 am

Re: Why No Split(String To Array)?

Post by AZJIO »

juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Why No Split(String To Array)?

Post by juergenkulow »

Code: Select all

; Access direct *ptr.Unicode with *ptr\u
s$="split,me,please"
slen=Len(s$)*2 
*sptr=@s$
ShowMemoryViewer(@s$,Len(s$)*2)
For i=0 To slen Step 2
  *ptr.Unicode=*sptr+i
  Debug Chr(*ptr\u)
Next 
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
BarryG
Addict
Addict
Posts: 4162
Joined: Thu Apr 18, 2019 8:17 am

Re: Why No Split(String To Array)?

Post by BarryG »

TheAutomator wrote: Mon Nov 07, 2022 11:29 pmAre there any good alternatives for the SpringField function that are not to complicated?
Use the forum search, because this has been discussed many times before. Searching for "stringfield slow" shows these:

viewtopic.php?p=568923
viewtopic.php?p=516077
viewtopic.php?p=502933
viewtopic.php?p=462072

And many others.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Why No Split(String To Array)?

Post by ChrisR »

It was indeed discussed many times with some nice code around :)
TheAutomator wrote: Tue Nov 08, 2022 12:08 am Also, it can not work with delimiters like "|-|" for example right?
I added a small change to mk-soft code to accept multi-characters delimiter
in his SplitString to list or array with option double-quotes (CSV)
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Why No Split(String To Array)?

Post by TheAutomator »

Thanks! very useful.
Yeah correct, a lot of stuff here about it.

I stumbled on a page with missing links for such a udf before I wrote this post.
Post Reply