About Multi-Dimensional Arrays...

BlitzMax Forums/BlitzMax Programming/About Multi-Dimensional Arrays...

Tani(Posted 2008) [#1]
I've been using that "array of an array" system for quite a while now for whenever I need to add additional dimension(s) to any empty arrays. But lately I've been taking note of that array example in the manual that seems way simpler and less tedious than the "array of an array" method. Though I never managed to get it to work on anything else than one-dimensional arrays since I started with BlitzMax so I asume it's a miss-leading information.

Well, to save you guys the work from looking it up in the manual yourself let me quote it:

Arrays may also be created 'on the fly' using the syntax:

New [ Dimension1 , Dimension2 etc... ]

This returns an array of the specified dimension(s) with each element initialized to Null. For example:
Local int_array:Int[]
int_array=New Int[10]

Well, based on that one would assume this would work too:
int_array=New Int[10,10]
But it doesn't; or have I missed something?

So is this a long-forgetten bug, limitation or something that should been corrected in the manual (adding a proper explanation of the use of "array of an array" in the process)?

It's a real shame you can't create multi-dimensional arrays that way though... For whenever you need to create a fresh, but simple multi-dimensional array that doesn't really require slicing.

Well, just felt like ranting about it, lol. Love BlitzMax otherwise. x3


Sledge(Posted 2008) [#2]
Try...
int_array:Int[,] = New Int[10,10]

Note the comma when you declare, hence kept over two lines it would be:

Local int_array:Int[,]
int_array=New Int[10,10]



SebHoll(Posted 2008) [#3]
Just thought I'd mention that a multi-dimensional array, and an array of arrays are two different things in BlitzMax:

Local multiDimArray:Int[,] = New Int[10,10]
Local arrayOfArray:Int[][] = [ [1, 2, 3], [4, 5, 6] ]
I use array of arrays more nowadays as they seem far easier to handle and manipule (e.g. slicing etc.) in BlitzMax, and they are more flexible.

Maybe it's just that they seem to make more sense to me...


Brucey(Posted 2008) [#4]
An actual Array of Arrays contains multiple single-dimension array objects, using the notation as Seb describes above.
A multi-dimensional array is a single block of indexed values stored in a significant sequence, as specified by the array dimensions.


Tani(Posted 2008) [#5]
So I DID miss something, thanks a lot! xD Good to know, as I prefer those when I don't need to modify the array later. Yes, I know arrays of arrays wasn't proper multi-dimensional arrays, but I always thought BlitzMax somehow didn't support that outside pre-declared arrays and that arrays of arrays was the only way to simulate proper multi-dimensional arrays when starting with an empty array. >_<

Somehow I never found examples of that, and aw, I where just missing that ",", never thought of trying that. =_= *sigh* BlitzMax could really do with some extra lines of explanation in the documentation, oh well. lol.

I'll keep using an array of arrays whenever I need slicing, but it always felt so unnecessary whenever I had an array that doesn't need to be sliced and modified later.

But I noticed in SebHoll's example that you can set both dimensions of an array of array at the same time... So apparently I've missed something there too. From other examples I guess that only works in the cases where you know the values and dimensions ahead though, which is usually not the case when I declare an empty array (not to mention using that on large arrays would require quite a lot of typing! xD).

Anyway, I might been doing it the hard way when it comes to array of arrays too then. So let me give an example of where I find multi-dimensional arrays more suitable than array of arrays, then maybe someone can direct me on a more suitable path again:

Piece of code using multi-dimensional array
SuperStrict

Local MyArray:Int[,]=New Int[20,20]

For Local f1:Int= 0 To 19; For Local f2:Int= 0 To 19
	MyArray[f1,f2]=f1*f2
	Print MyArray[f1,f2]
Next; Next

MyArray=New Int[10,10]	' <--- Resizing array, where old values are cleared

' modification of the array outside this not needed, ever

End
Similar piece of code using array of array
SuperStrict

Local MyArray:Int[][]

MyArray=MyArray[..20]
For Local f1:Int= 0 To 19
	MyArray[f1]=MyArray[f1][..20]
Next

For Local f1:Int= 0 To 19; For Local f2:Int= 0 To 19
	MyArray[f1][f2]=f1*f2
	Print MyArray[f1][f2]
Next; Next

' v--- Resizing the array, but the old values are kept (not always desirable) ---v
MyArray=MyArray[..10]
For Local f1:Int= 0 To 9
	MyArray[f1]=MyArray[f1][..10]
Next

' So have to be cleared in addition
For Local f1:Int= 0 To 9; For Local f2:Int= 0 To 9
	MyArray[f1][f2]=0
Next; Next

End
As you can see from these examples, such code became a lot easier to handle now that I know how to use proper multi-dimensional arrays. ^^; Array of arrays are useful in respective situations, but if they can't achieve the desired effect above in any simpler way wouldn't multi-dimensional arrays be most appropriate instead in those situations? Or is there a simpler way when using array of arrays?

Well, thanks again guys. This have helped me a lot. :3