Why No Split(String To Array)?
Posted: Mon Nov 07, 2022 11:29 pm
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:
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