Init 2D arry

BlitzMax Forums/BlitzMax Beginners Area/Init 2D arry

Grey Alien(Posted 2006) [#1]
Hi, I've figured out how to init an array on declaration:

myarray[] = [1,2,3]

But what is the syntax for a 2D array please:

myarray[][] = and then what?

Thanks.


rdodson41(Posted 2006) [#2]
myarray[][] = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]


Grey Alien(Posted 2006) [#3]
Thanks dude, I tried something like that but must have had it wrong!


Perturbatio(Posted 2006) [#4]
that is an array of arrays, but it will work just fine, an actual 2D array cannot be auto-initialised.

http://www.blitzwiki.org/index.php/Arrays


Grey Alien(Posted 2006) [#5]
Thanks for the link. So that's pretty weird then, I'm using an array of arrays not a 2D array and they can't be auto-initialised, weird!


Perturbatio(Posted 2006) [#6]
does this work for you?




Adam Novagen(Posted 2006) [#7]
Look, everyone, I've never used Blitz Max, so I could be totally wrong about this, but wouldn't the best way to initialize an array be to use something like:
Dim MyArray(100,100)
Replacing the 100's with whatever number of elements you need? I mean, the only time I create an array in a variable is when I do it in a type field, like:
Type MyType
    Field ArrayField[100]
End Type



Perturbatio(Posted 2006) [#8]
but wouldn't the best way to initialize an array be to use something like


No, that would be one way to declare a 2D array in B2D or B3D.

To declare a 2D array in BMax you would do

Global myArray:Int[100,100]


But that's not the same as initializing it, since you can't auto-initialize a multi-dimensional array.


bradford6(Posted 2006) [#9]
array of arrays needs to be initialized. a little more work but they are more flexible (can slice)

' =======2D Array=======
Local array[10,10]

array[2,2] = 4
Print array[2,2]

array[5,5] = 25
Print array[5,5]

' ==========Array of Arrays===========
Local GRID[][]
Local GridWidth:Int = 10
Local GridDepth:Int = 15

Grid = Grid[..GridWidth]

For Local count:Int = 0 To GridWidth - 1
		GRID[count] = Grid[Count][..GridDepth]
Next
	
GRID = GRID[..GridWidth][..GridDepth]



GRID[2][2] = 4
Print GRID[2][2]

GRID[5][5] = 25
Print GRID[4][4]




Grey Alien(Posted 2006) [#10]
In the end I did this:

	Const GAP=20
	Field StarCoords%[][] = [[-GAP*2,0],[-25,GAP+4],[0,35],[25,GAP+4],[GAP*2,0]] 

and accessed with StarCoords[x][y]

Seemed fine.


xlsior(Posted 2007) [#11]
Hi, I've figured out how to init an array on declaration:

myarray[] = [1,2,3]



Is this still supposed to work with the latest BlitzMax?
I could really use something like this, but when I try the above as-is it returns the error: Expected Expression but encountered '='


H&K(Posted 2007) [#12]
Global myarray[] = [1,2,3]
Or Local or Field


Dreamora(Posted 2007) [#13]
you better don't use that.
While this init look nice, its several times slower than

myarray[] = new int[3]
myarray[0] = 1
..
..


Seems like above is some kind of pseudo slicing mechanism doing the dynamic initialisation.


Grey Alien(Posted 2007) [#14]
weird. However, most of the time it's for relatively tiny Arrays and the first method is nice and readable.


Dreamora(Posted 2007) [#15]
I used it with 3 elements (position vector and therefor called on each object creation 2-4 times) and it ate a fair amount of time ... (we are talking of a factor of 20+ here already with 3 elements)

But will have to retest, my test on this issue was quite some time ago, perhaps some internal changes happened to it that do remove the need to use per element init.

I agree that the "c style" is more readable.


Grey Alien(Posted 2007) [#16]
wow that is a lot slower. My arrays like that are only initialised once on game init so it's a non-issue.