Changing "1,2,3" into an Int array
BlitzMax Forums/BlitzMax Beginners Area/Changing "1,2,3" into an Int array
| ||
What I'm trying to do is take a string of numbers:mynum$ = "1,2,3,4,5" and change it directly into an Int array like this: myarray:Int[] = [1,2,3,4,5] |
| ||
What about a function like:Local tmpString$ = "1,2,3,4,5" Local myArray:Int[] = StringToIntArray(tmpString) For Local tmpNum% = EachIn myArray Print "> " + tmpNum Next Function StringToIntArray%[]( pString$, pSeparator$ = "," ) Local tmpIntArray%[], tmpStringArray$[] = pString.Split(pSeparator) For Local tmpString$ = EachIn tmpStringArray tmpIntArray:+[Int(tmpString)] Next Return tmpIntArray EndFunctionShould hopefully speak for itself... |
| ||
SuperStrict
Local mynum:String="1,2,3,4,5"
Local outstring:String[]=mynum.split(",")
Local myarray:Int[outstring.length]
For Local x:Int=0 To outstring.length-1
myarray[x]=Int(outstring[x])
Print myarray[x]
Next
|
| ||
| Very nice thx. |
| ||
Small problem with yours tonyg...there's 5 numbers but the length of myarray ends up being 4. Just has to take out the "-1" on this line:Local myarray:Int[outstring.length-1] Here's my final version and thanks to you both:
Local array%[] = StrToIntArray("1,2,3,4,5")
For i = 0 To array.length-1
Print array[i]
Next
End
Function StrToIntArray%[](str$, sep$=",")
Local strArray$[] = str.split(sep)
Local intArray%[strArray.length]
For Local i:Int = 0 To strArray.length-1
intArray[i] = Int(strArray[i])
Next
Return intArray
End Function
|
| ||
| Noticed that and edited it... but not in time obviously. |