Returning the size on Multi-Dimensional Arrays

Monkey Forums/Monkey Programming/Returning the size on Multi-Dimensional Arrays

Loofadawg(Posted 2013) [#1]
So the .Length method returns the size of an array.

How do you get the length of the the other dimension ?

I understand that the first 5 elements in the first dimension = 8.
I was expecting five, but after thinking about it i guess that makes sense. But how would find the length for the next dimension (which is eight is this example) or the previous which would be 5.

I am wanting to use length in the creation of the multi-dimensional array creation and then I just got completely confused.

Strict

Import mojo

Class MyApp Extends App

	Field grid:Int[][][]
	
	

	Method OnCreate:Int()
		SetUpdateRate(60)
		
		grid = grid.Resize(5)
	
		For Local i:Int = 0 Until grid.Length()
			grid[i] = New Int[8][]
			For Local j:Int= 0 to 7
				grid[i][j] = New Int[8]
			End
		End
		
		 For Local h:Int = 0 To 4
			For Local i:Int = 0 to 7
				For Local j:Int = 0 to 7
					If (i=0) Or (j=0) Or (i=7) Or (j=7)
						grid[h][i][j] = 1
					Else
						grid[h][i][j] = h
					End
				End
			End
		End
		
		Return(0)
	End
	
	Method OnUpdate:Int()
		
		Return(0)
	End
	
	Method OnRender:Int()
		Cls(0,0,0)
		SetColor(255,255,255)
		For Local i:Int = 0 To 7
			For Local j:Int = 0 To 7
				DrawText(grid[4][i][j],i*16,j*16)
			End
		End
		
		DrawText(grid[0].Length(),200,0)
		DrawText(grid[1].Length(),200,16)
		DrawText(grid[2].Length(),200,32)
		DrawText(grid[3].Length(),200,48)
		DrawText(grid[4].Length(),200,64)
		
	
		Return(0)
	End
	
End

Function Main:Int()
	New MyApp
	Return(0)
End	



Loofadawg(Posted 2013) [#2]
I read the thread:

http://www.monkeycoder.co.nz/Community/posts.php?topic=6170

but, Oh! does that make my head hurt. ;^)


Loofadawg(Posted 2013) [#3]
Ah! Figured it out. I keep forgetting to close the MServer window when compiling for HTML5 and so my results didn't change.

grid.Length for the 1st dimension
grid[].Length for the 2nd
grid[].Length for the 3rd.

Now that makes perfect sense. Phew! Hate it when I don't "get" something but such a great feeling when you figure it out.

Baby steps. Baby. Steps.


Gerry Quinn(Posted 2013) [#4]
Assuming the array is properly filled out (at least one element in each dimension), I use:

arr.Length() ' first dimension
arr[ 0 ].Length() ' second dimension

Of course Monkey has the possibility of having different lengths for sub-arrays (you might conceivably use this for a triangular matrix or something) but I think it is rarely used. In any case, to find the length of sub-arrays in such an array, you would have to query them independently in the same way.