Page 1 of 1

Why No Split(String To Array)?

Posted: Mon Nov 07, 2022 11:29 pm
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

Re: Why No Split(String To Array)?

Posted: Mon Nov 07, 2022 11:54 pm
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   


Re: Why No Split(String To Array)?

Posted: Tue Nov 08, 2022 12:08 am
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 :)

Re: Why No Split(String To Array)?

Posted: Tue Nov 08, 2022 1:17 am
by idle

Re: Why No Split(String To Array)?

Posted: Tue Nov 08, 2022 2:09 am
by AZJIO

Re: Why No Split(String To Array)?

Posted: Tue Nov 08, 2022 4:30 am
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 

Re: Why No Split(String To Array)?

Posted: Tue Nov 08, 2022 9:09 am
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.

Re: Why No Split(String To Array)?

Posted: Wed Nov 09, 2022 10:29 am
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)

Re: Why No Split(String To Array)?

Posted: Wed Nov 09, 2022 11:15 am
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.