String.split suggestion

BlitzMax Forums/BlitzMax Programming/String.split suggestion

JoshK(Posted 2007) [#1]
s$="Blitz Max"
Local sa$[]
sa=s.split(" ")
Notify sa[1]

I recommend making it so it will ignore double-spaces if space is the divider.


Russell(Posted 2007) [#2]
That could be useful as a parser.

On a similar note, I thought it might be cool if you could use "+" (or something similar) with slices to create a new array:

Global a[] = [1,2,3,4,5]
a = a[..2] + a[4..]

to create a new array with [3] missing and a.length shortened by one. It's not that hard to do it in other ways, but this would be useful.

Russell


marksibly(Posted 2007) [#3]
Hi,

Use "" (empty string) to split by whitespace sequences.


SebHoll(Posted 2007) [#4]
On a similar note, I thought it might be cool if you could use "+" (or something similar) with slices to create a new array:

Global a[] = [1,2,3,4,5]
a = a[..2] + a[4..]

to create a new array with [3] missing and a.length shortened by one. It's not that hard to do it in other ways, but this would be useful.

I thought this was one of the new features in v1.26.


JoshK(Posted 2007) [#5]
Nice!


marksibly(Posted 2007) [#6]
Yeah, sorry about lack of docs - it was slipped in at the last minute to fix a bug when splitting with an empty string.


dmaz(Posted 2007) [#7]
you know what I really miss from my perl days....

(var1,var2,var3) = somefunctionthatreturnsanarray()

I find uses for it all the time, the most obvious would be:

local (red,green,blue) = GetColor()


JoshK(Posted 2007) [#8]
Just do this:

somefunction(varptr red,varptr green,varptr blue)


marksibly(Posted 2007) [#9]

you know what I really miss from my perl days....

(var1,var2,var3) = somefunctionthatreturnsanarray()


Quite a few scripting languages these days support this - I think such 'auto structs' are called 'tuples'.

I've never used a language that supported tuples myself, but they look pretty cool.


dmaz(Posted 2007) [#10]
Just do this:

somefunction(varptr red,varptr green,varptr blue)
that's what I sometimes do but exactly what I want to avoid because it's rarely appropriate or even readable. that GetColor while the most obvious was not presented the best for an example. this is better and all would work under this method.

local (red,green,blue) = GetColor( color:int )
local (red,green,blue,alpha) = GetColor( color:int )
local alpha = GetColor( color:int )[3] ' though we can do that now

using var can get messy and you might have to resort to a type or just returning the array and setting the vars. allowing sequential assignments of an array is also awesome for reading files and databases.


dmaz(Posted 2007) [#11]
I've never used a language that supported tuples myself, but they look pretty cool.
I hope that translates into "I'll see if it's possible for Max" :)

[edit] tuples.... I didn't notice that or know that. I got too excited about the "cool" comment.


Russell(Posted 2007) [#12]
@SebHoll: I haven't downloaded 1.26 because it seemed like some people were having issues with it, so I wanted to wait. Good to know it's in there, though!

Since we're talking about features that would be cool, here's one that used to be in Blitz 2 for the Amiga:
Type blah
   Field a
   Field b
End Type

Test.blah = New blah
Test = 3

(I may be off a bit on the syntax...it's been a few years...). But anyway, that last line would default to the first field when no "\" was used (a would equal 3). In fact, I think you could even go:

Test = 3,4

where both a and b would get their values changed. Could be useful if you imagine a type with a dozen or more fields in it:

Test = 3,4,"Hello",7.9,"Tuesday"

instead of:

Test\a = 3
Test\b = 4
Test\c.s = "Hello"
Test\d# = 7.9
Test\e.s = "Tuesday"

(Don't know if mixing base types in the shortcut example would cause confusion, but as long as they matched their respected fields I guess it wouldn't matter - probably not the 'best' programming practice...)

Russell