dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Simple Array Problem...

  1. #1
    Senior Member maksum's Avatar
    Join Date
    Oct 2002
    Location
    Portland, OR
    Posts
    118

    Simple Array Problem...

    Ok, I'm just having the hardest time wrapping my brain around this. Hopefully someone can help me.

    So I want to create an array (myTestArray2), and make it a duplicate of an existing array (myTestArray). I then want to empty out the original array (myTestArray) but keep the new one intact.

    You can see from this code that when I trace myTestArray2 in the end it is empty... yet I didn't even touch that one in my loop.

    Code:
    myTestArray = new Array(5,6,7,8);
    trace("myTestArray = " + myTestArray);
    myTestArray2 = myTestArray;
    trace("myTestArray2 = " + myTestArray2);
    testItemsToShift = myTestArray.length;
    for(i=0;i<testItemsToShift;i++){
      trace(i);
      myTestArray.shift();
    }
    trace("myTestArray = " + myTestArray);
    trace("myTestArray2 = " + myTestArray2);
    So how do I create myTestArray2, make it a copy of myTestArray, but then have it left alone while I manipulate the original myTestArray?

    Thanks much in advance.

    Mike

  2. #2
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    code:
    myTestArray = new Array(5,6,7,8);
    trace("myTestArray = " + myTestArray);
    myTestArray2 = myTestArray;
    trace("myTestArray2 = " + myTestArray2);
    /*
    testItemsToShift = myTestArray.length;
    for(i=0;i<testItemsToShift;i++){
    trace(i);
    myTestArray2.shift();
    }*/
    myTestArray = new Array();
    trace("myTestArray = " + myTestArray);
    trace("myTestArray2 = " + myTestArray2);


  3. #3
    Senior Member maksum's Avatar
    Join Date
    Oct 2002
    Location
    Portland, OR
    Posts
    118


    sorry I wasn't more clear. This was my effort to take it down to it's simplest form, but what I'm ACTUALLY doing to myTestArray (the original one) is pulling out it's elements one by one and doing something with them... so I need to know how to maintain myTestArray2 while I run the original through that loop.

    I actually figured out a way to do it. Instead of creating myTestArray2 my simply stating that it equals myTestArray I run it through a little loop copying out it's elements one by one. Certainly makes sense, I was just hoping for something a little simpler.

    It's odd... you would think the original script would work. But instead of just creating an immediate freeze-frame duplicate, it creates a dynamic, real-time duplicate... odd indeed.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center