What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Curiousity with arrays (meaning, why is this happening?

Curiousity with arrays (meaning, why is this happening?
Author: Message:
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. Curiousity with arrays (meaning, why is this happening?
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.
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
01-10-2008 05:13 PM
Profile PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Curiousity with arrays (meaning, why is this happening?
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

This post was edited on 01-10-2008 at 05:40 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
01-10-2008 05:39 PM
Profile E-Mail PM Web Find Quote Report
Volv
Skinning Contest Winner
*****

Avatar

Posts: 1233
Reputation: 31
35 / Male / Flag
Joined: Oct 2004
RE: Curiousity with arrays (meaning, why is this happening?
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[].

This post was edited on 01-10-2008 at 05:45 PM by Volv.
01-10-2008 05:39 PM
Profile PM Find Quote Report
mynetx
Skinning Contest Winner
*****

Avatar
Microsoft insider

Posts: 1175
Reputation: 33
37 / Male / Flag
Joined: Jul 2007
RE: Curiousity with arrays (meaning, why is this happening?
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.
mynetx - Microsoft, enhanced.

You have a problem or issue with Windows, Internet
Explorer or Office?
Send a tweet!
01-10-2008 07:15 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On