About Monkey 2 › Forums › Monkey 2 Programming Help › How to init array?
This topic contains 12 replies, has 9 voices, and was last updated by 
 Danilo
 2 years, 1 month ago.
- 
		AuthorPosts
 - 
		
			
				
January 9, 2017 at 1:51 pm #6335
Can’t seem to nail this, so what is the MX2 equivalent of :
Local score:Int[]=[10,20,30]
January 9, 2017 at 1:53 pm #6336Monkey123Local MyArray:Int[] = New Int[](1, 2, 3)' I think. xDJanuary 9, 2017 at 1:57 pm #6337Thanks scurty! Is it documented somewhere?
January 9, 2017 at 3:59 pm #6342Somewhere on the forum is..
January 9, 2017 at 6:30 pm #6347… if only there was a wiki for all this stuff…
January 10, 2017 at 1:36 am #6361… if only there was a wiki for all this stuff…
This is fundamental stuff that *should* be in the language reference, not a wiki.
It’s my fault it isn’t of course, and I’ll be spending some time soon updating the docs.
February 28, 2017 at 8:43 am #7321So how can I init a 2d array?
Monkey1234567Global map:Int[,]map = New Int[10,10]'Now I'd like to fill that array line by line, something like:map[,0] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 'doesn't work obviously but how can I achieve that?map[,1] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]edit: Found the solution: http://monkey2.monkey-x.com/forums/topic/way-to-predefineinline-define-an-array-of-arrays/
February 28, 2017 at 10:37 am #7326field _tracks:Track[, ] = New Track[64, 8]
tried to do the fill using different methods, but none worked. best solution would be to use data and read it into the array?
February 28, 2017 at 5:44 pm #7329@xaron:
Maybe you could use array-of-arrays for this case?Monkey1234567891011121314151617181920212223242526272829303132333435#Import "<std>"Using std..Function Main()Global map := New Int[][10]'Now I'd like to fill that array line by line, something like:map[0] = New Int[]( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )map[1] = New Int[]( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 )map[2] = New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )map[3] = New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )map[4] = New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 )map[5] = New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 )map[6] = New Int[]( 0, 0, 0, 8, 8, 8, 8, 0, 0, 0 )map[7] = New Int[]( 0, 0, 8, 0, 0, 0, 0, 8, 0, 0 )map[8] = New Int[]( 0, 8, 0, 0, 0, 0, 0, 0, 8, 0 )map[9] = New Int[]( 8, 0, 0, 0, 0, 0, 0, 0, 0, 8 )For Local i := 0 Until 10Local out:String = ""For Local j := 0 Until 10out += String( map[i][j] ) + " "NextPrint outNextEndFebruary 28, 2017 at 9:37 pm #7330There’s currently no way to init 2d arrays like this sorry. You’ll need to use an array of arrays, ala Danilo suggestion, or manually initalize elements one by one.
I do plan to add this eventually (and N-d copies, slices etc) but it’s a little tricky due to c++-isms, and with 3d, 4d arrays etc the syntax gets pretty weird.
March 1, 2017 at 5:12 am #7342yay slices. Best of luck
March 1, 2017 at 8:21 am #7343Thanks guys!
March 1, 2017 at 9:39 am #7345Another way for initializing an array-of-arrays, provided by Mark himself:
Monkey123456789101112131415161718192021222324252627282930313233#Import "<std>"Using std..Function Main()Global map := New Int[][] (New Int[]( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ),New Int[]( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 8, 8, 8, 8, 0, 0, 0 ),New Int[]( 0, 0, 8, 0, 0, 0, 0, 8, 0, 0 ),New Int[]( 0, 8, 0, 0, 0, 0, 0, 0, 8, 0 ),New Int[]( 8, 0, 0, 0, 0, 0, 0, 0, 0, 8 ) )For Local i := 0 To 9Local out:String = ""For Local j := 0 To 9out += String( map[i][j] ) + " "NextPrint outNextEndGood example for the MX2-CODE-COLLECTION.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.