2D Arrays.

BlitzMax Forums/BlitzMax Beginners Area/2D Arrays.

Paul "Taiphoz"(Posted 2005) [#1]
Hay all.

ok so I needed to make a 2D Array, now TBH with you I couldnt get it to work, I was trying something like this.

Global MyArray:int[][]=[[1,2][3,4]]
Print MyArray[1][1]

What I was expecting was "3" as an output or "1 3" but what I got was "4" the above example is taken from another array post in which he used strings but the idea is the same.

Anyway What I actually want would look like this in Blitz3D.

Dim Array$(2,2)
Array$(1,1)="1"
Array$(1,2)="2"
Array$(2,1)="3"
Array$(2,2)="4"
So if I wanted 3 Id do this.

Print Array$[2,1]

I have now worked out how to do this in Max and thought I would share it.

Global Hscore$[5+1,2+1]
For looper%=1 To 5
	Hscore$[looper,1]="Name-"+String(looper)
	Hscore$[looper,2]=String(100*looper)
Next

For looper%=1 To 5
	Print Hscore[looper,1]+":"+Hscore[looper,2]
Next


What the above Bmax code does is create a 2D Array, with 5 Elements in the first section and 2 in the last, I will be using this as a high score array, 5 positions and storing a name and socre in each.

My question at this point is what the hell is the +1 for. ??

GOOD TIP <<<<

To get the above code what I did was code it in B3D and then imported it to max to see how it wanted it done.


tonyg(Posted 2005) [#2]
I think it's because array sizes are exactly as numbered
i.e a[5] will create 0-4.
rather than B3D giving 0-5.
To loop 1 to 5 they've simply added an extra element to the array.


Damien Sturdy(Posted 2005) [#3]
i think the way max does arrays is wrong. any form of basic has always used 0-value. Some even having a MIN perameter for the dim statement.

if you do the equivalent of Dim Array(5) in max, if you try to access the 5th element, you get bombed. I just found that a bit odd to be honest.


Oh well, not particularly hard to fix :/


Dreamora(Posted 2005) [#4]
what basic does, does no one really interest

all real languages have arrays like used in max from 0 to number_of_elements - 1

the way blitbasic did it was just wrong! if you declare an array with 5 elements it has to have 5 elements not 6

there is another reason for this behavior: Streams etc are adressed with [x] as well where x is the memory position which starts at 0 and not at 1 ;)


Paul "Taiphoz"(Posted 2005) [#5]
Dreamora is right here the Array Should start at 0 I was only wondering why max Added the +1