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:
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
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Curiousity with arrays (meaning, why is this happening? - by MeEtc on 01-10-2008 at 05:13 PM
RE: Curiousity with arrays (meaning, why is this happening? - by Matti on 01-10-2008 at 05:39 PM
RE: Curiousity with arrays (meaning, why is this happening? - by Volv on 01-10-2008 at 05:39 PM
RE: Curiousity with arrays (meaning, why is this happening? - by mynetx on 01-10-2008 at 07:15 PM


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