List of numbers into an array

Monkey Forums/Monkey Programming/List of numbers into an array

SuperDan(Posted 2012) [#1]
How do I read a large list of comma separated values into an array? (3000 values for example).

Should I store the CSV's in a separate file to read at runtime (if so, what file type?) or should I incorporate a Data list of some sort into my code (as in Basic from the old days, which is just about my limit, I'm ultra noob).

If anyone can point me to an example, I would be very grateful. I posted something similar in an earlier topic but I confused myself as to what I wanted, so probably confused anyone else reading the post.

Thanks muchly,
Dan.


MikeHart(Posted 2012) [#2]
string.Split will take a string, split it at the character your chose and return an array with the values of your line.

Let's say loaded that file into a string...

Local lines := yourTotaldata.Split(String.FromChar(10))
For Local line:= Eachin lines
    If line = "" Then Continue
    Local data$[] = line.Split(",")
    'Now process the elements of the data$ array
Next


It is not a complete example but may give you a start.


SuperDan(Posted 2012) [#3]
Hi,

Thanks for the reply. I have tried incorporating your idea into some runnable code but nothing I try seems to work. I have this at the moment...

Local totaldata:String = LoadString("text.txt")
Local lines = totaldata.Split(String.FromChar(10))
For Local line:= Eachin lines
   If line = "" Then Continue
   Local data$[] = line.Split(",")
Next	
Print data[100]	

I get a compile error saying cannot convert string to int on line 2 above.

Any help or further explanation much appreciated.


muddy_shoes(Posted 2012) [#4]
That's because:
Local lines = totaldata.Split(String.FromChar(10))


defines lines as an Int as the default. You want:
Local lines:String[] = totaldata.Split(String.FromChar(10))


Bloggy thing about this here: http://pointlessdiversions.blogspot.co.nz/2012/04/monkey-tips-default-types-and-implicit.html


SuperDan(Posted 2012) [#5]
Thanks muddy, but now the error has changed to "cannot convert from String[] to string.

Also, what does the FromChar(10) part do?

Sorry for being a rubbish noob.

Thanks,
Dan.


muddy_shoes(Posted 2012) [#6]
Yes, I missed the array. Sneakily edited it in.

Edit:

Char 10 is the newline character: http://www.asciitable.com/


SuperDan(Posted 2012) [#7]
Magic, thanks for the info muddy.

Now I have this

Local totaldata:String = LoadString("text.txt")
Local lines:String[] = totaldata.Split(String.FromChar(10))
For Local line:= Eachin lines
   If line = "" Then Continue
   Local data$[] = line.Split(",")
Next	
Print data[100]	


...but I get the error "Identifer 'data' not found." I guess I need to set up 'data' earlier but I'm not sure how.

Thanks.


muddy_shoes(Posted 2012) [#8]
The issue is that data is defined inside the For loop and you're trying to access it outside of the loop. If you move the print line inside the loop, after the "Local data$[] = ..." line then it might work if you have that many elements.

Is that literally all the code that you're trying to compile? If not, can you post the whole thing in a codebox?


SuperDan(Posted 2012) [#9]
LOL, thanks again muddy. I am a SuperNoob but I do actually have a full program to compile.

I set up the data variable outside the For loop and now it works. Super, smashing. Here is the code now.

Local totaldata:String = LoadString("text.txt")
Local lines:String[] = totaldata.Split(String.FromChar(10))
Local data$[]
For Local line:= Eachin lines
    If line = "" Then Continue
    data = line.Split(",")
Next	
Print data[81]	


Great work, thank you so much!
Dan.


skid(Posted 2012) [#10]
If you want to keep it optimal go with "a data list":

Global a[]=[
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	1,2,3,4,5,6,7,8,9,10,
	5,6,7]

Function Main()
	Print "hello"+a.Length
End



MikeHart(Posted 2012) [#11]
That's because:

Local lines = totaldata.Split(String.FromChar(10))

defines lines as an Int as the default.


If you do
Local lines := totaldata.Split(String.FromChar(10))


It will automatically take the type from the expression that is assigned to it. Split will return a string array.
Because of the ":=" operator, lines will become a string array.


SuperDan(Posted 2012) [#12]
This is all great info, thanks guys.