Creating an array of strings
BlitzMax Forums/BlitzMax Beginners Area/Creating an array of strings
| ||
OK, this has been driving me crazy. All I want to do is create an array of strings but I can't work out what the syntax should be. I just want to set up an array and be able to assign strings to each element. e.g. myarray[1] = "A string" myarray[2] = "Another string" myarray[3] = "yeah, you get the idea" Please note that what I'm trying to do can not be done with autoarrays since I will not know the array contents when it is created. I'm sure this should be simple, but I just can't work it out. |
| ||
Strict Local myarray:String[3] myarray[0] = "A string" myarray[1] = "Another string" myarray[2] = "yeah, you get the idea" For Local i = 0 To 2 Print myarray[i] Next Or if you don't know the final size of the array: Strict Local myarray:String[] myarray = myarray[..myarray.length+1] myarray[myarray.length-1] = "A string" myarray = myarray[..myarray.length+1] myarray[myarray.length-1] = "Another string" myarray = myarray[..myarray.length+1] myarray[myarray.length-1] = "yeah, you get the idea" For Local i = 0 To myarray.length-1 Print myarray[i] Next |
| ||
Hmm, I tried that but it didin't seem to work. I'll have another crack at it - must be my dodgy coding ;-) |
| ||
Heres another way thats a bit less typing.Strict Local myarray:String[]=["A string","Another string","yeah, you get the idea"] For Local i = 0 To 2 Print myarray[i] Next |
| ||
OK, I got it working. In fact I had it right hours ago, but one of my variable names was wrong and throwing the whole thing out. Now there's a lesson why to use Strict! (I normally do, but in this one instance I did not and look what happened) |