Arrays
Monkey Forums/Monkey Programming/Arrays
| ||
Is it possible to define a 2 dimensional array of say 4 rows and 20 columns? If so could someone please tell me how? Thanks Gary |
| ||
no worries, I found the answerGlobal myarray:=[New Int[4],New Int[40]] Function Main() myarray[0][0]=20 myarray[0][1]=30 Print myarray[0][1] End |
| ||
That's not doing what you think, or at least not what you said you were trying to do.Global myarray:=[New Int[4],New Int[40]] This doesn't declare a two dimensional 4x40 array of ints. It declares an array with two elements, one of which is a four element array of ints and one is a forty element array of ints. What you want is: Global myarray:= [New Int[40],New Int[40],New Int[40],New Int[40]] |
| ||
with this code you have free choice of array size in both dimensions:' how to call DIM a:Int[4,40] in monkey: Global a:Int[][] =IntArray2D(4,40) 'this is only for demo purpose: Global X% For local i%= 0 to 3 For local j%= 0 to 39 X= a[i][j] Next Next end ' Function to DIM a 2-dimensional Array: Function IntArray2D:Int[][] (X%,Y%) Local tmp:Int[X][] For Local I% = 0 Until X tmp[I] = New Int[Y] Next Return tmp End Function |
| ||
Global array:Int[64] 'array[x+(8*y)] for an 8x8 array 'array[x+(16*y)] for a 4x16 array 'array[x+(32*y)] for a 2*32 array For y = 1 to 6 For x = 1 to 6 array[x+(8*y)] = 1 Next Next .. .. .. For y = 0 to 7 For x = 0 to 7 If array[x+(8*y)] SetColor 255,255,255 Else SetColor 127,127,127 End If DrawRect x*32,y*32,30,30 Next Next You can create a function to make sure everything stays within the confines of the dimensions. Not sure how efficient this is, but it works. |
| ||
cheers for the tips, as I mentioned in the IOS forum its strange how HTML5 never picked up on the error but IOS does. Least I know how to do it properly now Thanks |
| ||
its strange how HTML5 never picked up on the error but IOS does. Javascript arrays automatically resize as you add elements so it's not an error in that language. |
| ||
Also, the deeper you go, the more objects are created. This is bad if you're working with Android, or any heap-based language. [4][40] will create 5 objects, but [40][4] will create 41 objects. That's a lot. Don't even think of storing large levels for a tile-based engine like that! If you're going for performance, I'd suggest just sticking with a 1D array and indexing it arithmetically. |
| ||
Samah, have you tested the performance issues on the various platforms? I've done some testing on HTML5 (FF&Chrome), Flash and GLFW and I'm not seeing a major performance win to be had by doing your own index arithmetic. Even where there is a performance gain it doesn't seem worth the overhead of tracking the intended row sizes manually. The only places I can see some value are edge-cases, such as completely resetting the values in an Array, or perhaps if you were doing nothing but iterate over a large array, such as in a cellular automata. |
| ||
Am I right in thinking that 3 dimensional arrays are not possible with Monkey and I would have to use a 1D and index it? |
| ||
It's just an extension of the 2D case. Here's a helper function.Function AllocateArray:Int[][][]( i:Int, j:Int, k:Int, initial:Int) Local arr:Int[][][] = New Int[i][][] For Local ind = 0 Until i arr[ind] = New Int[j][] For Local ind2 = 0 Until j arr[ind][ind2] = New Int[k] For Local ind3 = 0 Until k arr[ind][ind2][ind3] = initial Next Next Next Return arr End |
| ||
Cheers Muddy |