[Closed] incrementally-named arrays
I’m trying to code a button to create incrementally-numbered arrays, so, for instance, pressing the button three times would create Array1, Array2, Array3, etc. Last night I tried the following:
i = 1
v[i][i] =[/i] #(1,2,3)
i+=1
v[i] = #(4,5,6,7,8)
i+=1
v[i] = #("A","B","C")
rollout c "Show"
(
spinner spn "array #" pos:[30,10] width:40 type:#integer range:[1,3,1]
spinner spn_2 "index #" pos:[100,10] width:40 type:#integer range:[1,3,1]
button btn "print"
on btn pressed do
(print v[spn.value][spn_2.value]
)
)
createDialog c 150 60
… and it worked just fine. I went to bed feeling rather smug.
This morning I’ve tried running the same script, and everytime a “v[i]” is encountered a No “put” function for undefined message is thrown. If I just plow ahead and press the “Print” button I get No “get” function for undefined.
[size=2]Can anyone instruct me on how to construct this properly? Many thanks.
[/size]
Not that I understand why you want to do this, but how about you start with telling MAXScript that ‘v’ IS an array?
v = #() --create an empty "master" array
i = 1 --set the index to 1
v[i] = #(1,2,3) --set the first sub-array of v to #(1,2,3)
...
Since you typed your code in global scope (bad idea, esp. when using single-character variable names which could be used by any other script), at a certain point you probably defined ‘v’ as array the first time you played with the idea and then v became a global variable for the duration of the session. The next day v wasn’t defined and you ended up with errors.
So start like
(--open local scope
local v = #() --create an empty "master" array in LOCAL scope
local i = 1 --set the index to 1 in LOCAL scope
v[i] = #(1,2,3) --set the first sub-array of v to #(1,2,3)
...
)--end script and local scope
This will ensure that the variable v will exist only within the local scope of your current script and will not be visible to any other script. These variables will be “seen” by your rollout which is one scope deeper and can see any variables in higher scopes and the global one.
If you really want to have the array visible to anyone, you could make it global, but in that case it is advisable to use a VERY descriptive name to avoid collisions with other unrelated code:
(--open local scope
global BHNH_MasterArrayForIncrementalStuff = #() --this is your own unique name in global scope
local i = 1 --set the index to 1 in LOCAL scope
BHNH_MasterArrayForIncrementalStuff[i] = #(1,2,3) --set the first array
...
)--end script and local scope
Great! Thanks, Bobo. I’m still inclined to overlook the global aspect of variables. It’s a habit I’ve got to break.