PDA

View Full Version : 2D arrays?


gopherCG
10-08-2003, 12:04 PM
Hi, I've just started on my city builder script. I've planned a lot before starting, and noticed:
arrayex[r][c] = [3,4]
doesn't work quite right (it runs, but acts strange)
I'm used to c++ code, so I expected this to work.
I found BigMatrix and nested arrays in help. But BigMatrix is for float only. And how do I access/read nested arrays?

Here's my attept at it:

macroScript Macro39
category:"DragAndDrop"
toolTip:""
(
--struct xy (x = 0.0, y = 0.0)
struct xy (x,y)

global rarray = #()
global carray = #()

Etemp = xy 0.0 0.0
for c = 1 to 5 do (carray[c] = Etemp)
for r = 1 to 10 do (rarray[r] = carray)

for r = 1 to 3 do
(
print r
for c = 1 to 4 do
(
print c
rarray[r][c] = [r,c]
print rarray[r][c]
)
)

print " "

for p = 1 to 3 do
(
print p
--print rarray[p].carray[4] --this line causes error
for q = 1 to 4 do
(
print q
print rarray[p][q]
)
)
)

Bobo
10-08-2003, 05:10 PM
The following comes straight from the upcoming MAXScript 6.0 Reference.

How do I create a multi-dimensional array?
MAXScript Frequently Asked Questions

A MAXScript Array is a one-dimensional list of elements. An element of an array is addressed by its index in brackets,

FOR EXAMPLE

myArray = #(10,20,30,40,50,60,70,80,90,100)

myArray[5] --> will return 50, the fifth element of the array.

Since an array can be an element inside of another array, you can create multi-dimensional arrays as you desire by placing multiple arrays inside an array. Using an index in brackets, you can reference the sub-array, using another pair of brackets and an index you can access an element inside the sub-array,

FOR EXAMPLE

--create an array with two elements, each one an array with 10 elements.
myMultiDimArray=#(#(1,2,3,4,5,6,7,8,9,10),#(10,20,30,40,50,60,70,80,90,100))
--add a third element to the array containing 10 more elements
append myMultiDimArray #(100,200,300,400,500,600,700,800,900,1000)

myMultiDimArray[2][5] --> returns 50-the 5th element of the 2nd sub-array

gopherCG
10-08-2003, 05:34 PM
Ah, interesting. I'm using max 4 now, waiting for max 6.

The reference confirms my use of arrays in my practice code, but I'm using for loops to first, assign positions values (xy struct), then, outputing all the positions.

If you run this, the final - print rarray[p][q] will give something strange like this:

1 --p
1 --q
[3,1] --position in current rarray[p][q]
2 --q iterating
[3,2]
3 --q iterating
[3,3]
4 --same
[3,4]
2 --p = 2 iterating
1 --q start over as usual
[3,1]
2
[3,2]
3
[3,3]
4
[3,4]
3 --p = 3 iterating
1 --q start over
[3,1]
2
[3,2]
3
[3,3]
4
[3,4]

well, p and q, are the same as r and c. And I'm troubled why the r (rows) all equal 3? which is the last iteration of r.
In contrast, the q works great.

LFShade
10-08-2003, 07:56 PM
This is happening because Maxscript essentially passes everything by reference. When you do this:

for r = 1 to 10 do (rarray[r] = carray)

you are assigning a reference to carray to each element of rarray. So, whenever you change carray the changes are reflected in every carray in rarray. Here's an example of a better, simpler way to construct a 2d array:

arr = #()
for i = 1 to 5 do arr[i] = #()

This way you're assigning new, empty "rows" to each "column" of the array.


RH

Bobo
10-08-2003, 08:42 PM
Originally posted by LFShade
This is happening because Maxscript essentially passes everything by reference. When you do this:

for r = 1 to 10 do (rarray[r] = carray)

you are assigning a reference to carray to each element of rarray. So, whenever you change carray the changes are reflected in every carray in rarray. Here's an example of a better, simpler way to construct a 2d array:

arr = #()
for i = 1 to 5 do arr[i] = #()

This way you're assigning new, empty "rows" to each "column" of the array.

RH


Absolutely right!

Just a note to the original author, you can use

print rarray

to see the complete content of the array at a glance.
Then everything becomes clear...

Cheers,
Bobo

gopherCG
10-08-2003, 11:32 PM
It's working thanks!

CGTalk Moderation
01-16-2006, 07:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.