Accessing Arrays correctly

BlitzMax Forums/BlitzMax Programming/Accessing Arrays correctly

taumel(Posted 2005) [#1]
Hi,

i do have a few questions to arrays:

a) How do i define an multidimensional array and access specific values in them?

Okay i have this working:

local dum:float[][]=[[1.0,2.0,3.0,4.0,5.0,6.0],[7.0,8.0,9.0,10.0,11.0,12.0],[13.0,14.0,15.0,16.0,17.0,18.0]]
print dum[2][5]

But can i also define it like this?

local dum:float[3,6]=?

or

local dum:float[3,6]
dum[0]=[1.0,2.0,3.0,4.0,5.0,6.0]
dum[1]=[7.0,8.0,9.0,10.0,11.0,12.0]
dum[2]=[13.0,14.0,15.0,16.0,17.0,18.0]

Same way of accessing specific values in such a scenario?


b) If defining longer lists for an array is there any continue sign so that i can break up my code for better readability?

I mean something like a "/" in this example:

local dum:float[][]=[/
[1.0,2.0,3.0,4.0,5.0,6.0],/
[7.0,8.0,9.0,10.0,11.0,12.0],/
[13.0,14.0,15.0,16.0,17.0,18.0]]

Btw it would really be nice if i also could pass "1" or a "1." instead of "1.0" when using floats.


Greetings,

taumel


Azathoth(Posted 2005) [#2]
'..' continues a line.


taumel(Posted 2005) [#3]
Thanks!


FlameDuck(Posted 2005) [#4]
Btw it would really be nice if i also could pass "1" or a "1." instead of "1.0" when using floats.
You can use 1# or 1:Float.

But can i also define it like this?
No. While they may seem similar in functionality, an array of arrays is semanticly very different from a matrix (or 2 dimensional array).

Same way of accessing specific values in such a scenario?
You can do something like this (ripped from project, but you should be able to get the idea):
Local aWidth = 3
Local aHeight = 6
Local rows:String[][]
Local rows = New String[][aHeight]
For Local i:Int = 0 Until rows.length
	rows[i] = New String[aWidth]
Next
Replace New String etc. with your initialization values (mine reads from a file, your needs may vary).


taumel(Posted 2005) [#5]
You can use 1# or 1:Float.

This looks really odd.


Thanks,

taumel


WendellM(Posted 2005) [#6]
But can i also define it like this?

local dum:float[3,6]=?

Depends if you want a multi-dimensional array, or an array of arrays. Multi-dimensional is the accessing method that I'm used to:

Strict

Local foo:Float[3,3]
Local i:Float

For Local x = 0 To 2
	For Local y = 0 To 2
		i# :+ 1.0
		foo[x,y] = i
		Print "foo["+ x + "," + y + "] = " + foo[x,y]
	Next
Next


The old Data-style approach is still available (and by using ReadData, you don't have to put ".", ".0", or "#" for floats in DefData):

Strict

Local bar:Float[3,3]

For Local x = 0 To 2
	For Local y = 0 To 2
		ReadData bar[x,y]
		Print "bar["+ x + "," + y + "] = " + bar[x,y]
	Next
Next

DefData 10, 11, 12
DefData 13, 14, 15
DefData 16, 17, 18

I suppose it depends on what you're doing that'll determine if you want one multi-dimensional array or an array of arrays.


taumel(Posted 2005) [#7]
Thanks!