.split

Archives Forums/BlitzMax Bug Reports/.split

CS_TBL(Posted 2009) [#1]
Sorry if asked before etc. etc.

Print Len("1;2;3;".split(";"))

I expected 3, not 4. Yes, I can easily wipe the last ; .. but, dunno.. I expected 3, not 4. :) It would be more consistent, somehow. Think of script parsing where you make a consistency agreement to close each command with a semi colon.

How about a check whether the last separator is followed by something or nothing?

(max 1.34)


Tommo(Posted 2009) [#2]
I prefer 4 here...
It might be sometimes handy if it strips last null string for you. But what if the null part is important for someone else?
:)


CS_TBL(Posted 2009) [#3]
Then, what about:
Print Len("1;2;3;".split(";",1))

where the ,1 means something like: "string ends with separator"


Warpy(Posted 2009) [#4]
I find it more useful to have an empty string on the end for my purposes, so I suppose it's more useful to have extra information you can get rid of instead of dropping the empty string.

Just write a little function to get it to do what you want, because the built-in split function's never going to change:
Function numbits(str$,separator$)
	Local bits$[]=str.split(separator)
	If bits[Len(bits)-1]=""
		Return Len(bits)-1
	Else
		Return Len(bits)
	EndIf
End Function

Print numbits("2;3;4;",";")
Print numbits("2;2;4",";")



Brucey(Posted 2009) [#5]
It seems to work exactly how I would expect it to.

The result should always be "number of separators + 1".


plash(Posted 2009) [#6]
Or, if you don't ever want the ending element, just call a function on the array every time you split a string that removes the last Null element.


Warpy(Posted 2009) [#7]
Or:
Function trimarray$[](arr$[])
	n=Len(arr)
	start=0
	finish=n-1
	While start<n And (Not arr[start])
		start:+1
	Wend
	While finish>0 And (Not arr[finish])
		finish:-1
	Wend
	Return arr[start..finish+1]
End Function

Print ",".join(trimarray(";1;;2;".split(";")))



plash(Posted 2009) [#8]
Yes, or all of them.


Floyd(Posted 2009) [#9]
How about a check whether the last separator is followed by something or nothing?


If it is a separator then it has to be followed by something. And the empty string is a valid string, so what appears to be nothing is in fact an empty string.


Brucey(Posted 2009) [#10]
Like what Floyd said.


GfK(Posted 2009) [#11]
Current usage works for me.

Actually it'd scupper my game proper if Mark changed its functionality, as I use the Split() method loads.