About Monkey 2 › Forums › Monkey 2 Programming Help › Arrays with multiple dimensions
This topic contains 6 replies, has 3 voices, and was last updated by 
 abakobo
 12 months ago.
- 
		AuthorPosts
 - 
		
			
				
April 21, 2018 at 11:51 am #14443
Need some input
Trying to understand arrays in Monkey2, is this a good way to define 2D arrays or is there a better way?
Monkey12345678910111213141516171819202122232425262728293031Namespace myapp#Import "<std>"Using std..Function Main()' Create 1D array and define contentsLocal somearray := New Int[](1,2,3,4,5,6,7,8,9,10)' Create 2D array and define contentsLocal somearray2d := Create2D(New Int[](3,3,33 , 0 , 0 ,2 , 120, 3 ,1 , 1 , 3 ))' Test Ptr and VarptrLocal a : Int=1000Local b : Int Ptr = Varptr ab[0] *= 10 ' a is now 10000End' Function that takes a 1d array and returns a 2d array, used as syntaxtic sugar to create and define content of 2D arrays as there is no way to do it directly yetFunction Create2D:Int[,](args:Int[])Local w := args[0],h := args[1] ' We use the first two numbers to define the size of the 2d-arrayLocal result := New Int[w,h]For Local y := 0 Until hFor Local x := 0 Until wresult[x,y] = args[y*w+x+2]NextNextReturn resultEndApril 21, 2018 at 11:47 pm #14451I’d recommend putting w,h in separate params instead of in the args array. That way you can use the same idea for non-int arrays too if necessary.
Actually, you’ve just given me an idea for how to add this to mx2cc! This is a bit tricky because it also has to be able to deal with 3d, 4d, 5d etc arrays, and I had been playing with syntax for an ‘arrays within arrays’ style initializers, but it was all pretty ugly, would probably have require a new kind of separator or grouping syntax/token and was generally un-monkey-like.
But the simplest solution would be to allow users to initialize new ‘sized’ arrays, eg: New Int[2,2]( 1,2,3,4 ). I don’t think this breaks any existing syntax and should be fairly easy to do (I hope) and while it’s a little inconvenient having to specify sizes instead of inferring them from data (which I could still add later I guess) it’s an improvement over what’s currently available, ie: nothing.
April 22, 2018 at 12:53 pm #14461Sounds like a great idea!
April 22, 2018 at 9:05 pm #14475This has been added and is now available from the develop branch at github!
So you can now do this:
Local myArray:=New Int[2,2]( 1,2,3,4 )
You need to get number of initializers exactly right or a runtime error will occur.
April 23, 2018 at 3:28 am #14477That’s amazing
+ for QUICK SUPPORT.
Hate to ask for something else but would an optional sizing of 1-dimension-arrays be a bad idea?
Okay I admit that I ask mainly because one part of me love consistency but it would be an easy way to check that an array actually contains the correct amount of data when that matters.EDIT you were faster than me, it was already supported now.
April 23, 2018 at 4:17 am #14478Monkey123456789101112131415161718192021222324' Monkey2 v2018.04' Mx2cc v1.1.12' Ted2go v2.10' We have perfectly working multidimensional arrays!Namespace myapp#Import "<std>"Using std..Function Main()' Create 1D array and define contentsLocal myArray := New Int[10] ( 1,2,3,4,5,6,7,8,9,10 )' Create 2D array and define contentsLocal myArray2 := New Int[2,2] (1 , 2 ,3 , 4 )' Create 3D array and define contentsLocal myArray3 := New Int[2,2,2] ( 1,2 , 3,4 , 5,6 , 7,8 ) ' My naive way of trying to notate 3-dimensionsEndApril 24, 2018 at 1:32 pm #14495Great news!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.