PDA

View Full Version : a=#(3,2,1);b=a;b[1]=1;why a becomes #(1,2,1)


Gokhan
05-07-2009, 08:07 PM
I am new to both C and maxscripting, and stuck with a probably simple question;
I have an array such as a=#(3,2,1) and its values depends on some conditions,
I need to keep the original form of the array and change it in a "for loop" at each iteration depending on sub-conditions.
I equalized "a" to another variablename "b";
b=a;
and i changed b in the loop, but I saw at each iteration also a changes, so in the next iteration the array does not turn into its original form.
I thought that this culd be due to local-global variables and changed a as global and b as local and get the value of a with " b=globalVars.get #a" but the same thing happened.

How could I avoid this?

Thanks.

PiXeL_MoNKeY
05-07-2009, 08:20 PM
By doing b=a you are creating an instance, changing of one effects the other. If you want a unique copy, then you should use b=copy a #nomap.a=#(3,2,1)
b= a
b[1]=1
format "array a:%, array b:%\n" a b

a=#(3,2,1)
b= copy a #nomap
b[1]=1
format "array a:%, array b:%\n" a bcopy <array> [#noMap] -- mapped

Creates a copy of all the elements in the array. Returns a value of OK. If #noMap is specified, the copy made is what is called a shallow copy - only a copy of the upper-level value itself (that is, the array value) is created. Copies aren't made of values stored in the array, rather a reference to the same value is stored in both array values.-Eric

Gokhan
05-07-2009, 09:05 PM
Thanks a lot. It worked perfectly.

CGTalk Moderation
05-07-2009, 09:05 PM
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.