Shoutbox

Curiousity with arrays (meaning, why is this happening? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Curiousity with arrays (meaning, why is this happening? (/showthread.php?tid=80849)

Curiousity with arrays (meaning, why is this happening? by MeEtc on 01-10-2008 at 05:13 PM

I have the following code:

code:
function OnEvent_Initialize(MessengerStart){
var str = 'a;b;c;d;e;f;'
var data3 = new Array();

var data = str.split(';');
var data2 = data;
for(var i = 0;i<=5;i++){
   data3[i] = data[i];
}

data[0] = 'z';
data[1] = 'x';
data[2] = 'y';
data[3] = 'v';
data[4] = 'w';
data[5] = 'u';

Debug.Trace(data);
Debug.Trace(data2);
Debug.Trace(data3);
}
gives the following output:
code:
Script is starting
Script is now loaded and ready
z,x,y,v,w,u,
z,x,y,v,w,u,
a,b,c,d,e,f
Shouldn't the second line of letters be the same as the second? obviously, its not, and I'm trying to figure out why.
RE: Curiousity with arrays (meaning, why is this happening? by Matti on 01-10-2008 at 05:39 PM

Let's try to understand what happens.

  1. A string is defined with 5 letters in, separated by semi-colons.
  2. An array is created, data3.
  3. An array is created by splitting the string. So, the array will contain:
    code:
    [a,b,c,d,e,f]
  4. A copy of this array is stored as data2.
  5. data3 is filled with the first 5 elements of the data array.
  6. The content of data is changed.
The error is that when you assign an array variable to a new variable, it is dependent of this original array. That is, when you change the original one, the new one follows. When you change the second array, it becomes independent and will inherit all elements from the first array. After that, you can change both independently.

So, to make the data2 variable independent, you can use a nifty trick to make it immediately inherit the elements from the original array instead of making a reference to the first one:
code:
...
var data = str.split(';');
var data2 = data.slice(0);
for(var i = 0;i<=5;i++){
...
A small explanation here: the slice function returns a selected amount of elements from an array. You can specify a start and end position, but when you leave out the end parameter, it'll return the elements from the given start position to the end or the array. Result: all elements will be returned as a new array, and this returned array is saved as data2 variable.

Oh note: you may want to remove the final semi-colon after "f" in your string, as you have an empty element at the end of your arrays now. ;)

Reference: http://www.sematopia.com/?p=12
RE: Curiousity with arrays (meaning, why is this happening? by Volv on 01-10-2008 at 05:39 PM

This is because of the following line:
data2 = data;
What you are essentially doing is creating a reference to data in data2. So when you print data2[0] it's actually referring to data[0] and printing that.
In order to copy an array as opposed to referencing, you need to clone all the elements it contains.

EDIT: Ok, guess Mattike beat me to it with a far superior post *cough* Show off *cough* lol j/k :p

One thing I would like to point out:

quote:
Originally posted by Mattike
The error is that when you assign an array variable to a new variable, it is dependent of this original array. That is, when you change the original one, the new one follows.
I think Mattike already knows this but just expressed it ambiguously: it does not actually create a copy and then apply any changes made to the original to the new array, it only creates a reference to the original array. That is, that data2[] is essentially data[].
RE: Curiousity with arrays (meaning, why is this happening? by mynetx on 01-10-2008 at 07:15 PM

In C++, this technics is called pointers because the variables contain pointers to the memory sectors where the data are stored. So, "data2 = data;" just copies the address of the array to the second. Obviously, both pointers point now to the same memory contents.