4 Replies Latest reply on Nov 7, 2006 3:07 AM by Newsgroup_User

    Array Assignment

    TroyK Level 1
      This seems like it should be so simple.

      I want to assign b_ary with the values of a_ary (b_ary = a_ary) and then I want to make changes to b_ary without changing a_ary. But when I do anything with b_ary it also makes those changes in a_ary. As you can see in my example below it works with variables a and b. I said b = a and then changed the value of b without changing the value of a. But it doesn't work the same way with arrays. Is there a way I "copy" a_ary to b_ary?

      Thanks

      Troy


      //variable assignment
      var a = 20
      var b = a
      b = 7
      trace(a) //20

      //array assignment
      var a_ary:Array = new Array()
      a_ary.push({label:"a", data:1})
      a_ary.push({label:"b", data:2})
      var b_ary:Array = a_ary
      b_ary.push({label:"c", data:3})
      trace(a_ary[2].label) //c
        • 1. Re: Array Assignment
          WebXperience Level 1
          I'm not sure how it works in flash exactly, because flash doesn't support pointers. But what is happening is that when you assign the value of one variable to another only the values are assigned. But when you try to assign arrays in the way you are attempting the addresses of one array is assigned rather than the value. Try using a for loop and assign the values that way. Doing that you should be able to assign the values. You could even put the routine into a function and then call the function as in:

          function copyArray(var from:Array, var to:Array) {
          for (i=0; i<from.length; i++) {
          to .value = from.value;
          }
          }

          Then you could call the function like this:

          copyArray(a,b);
          • 2. Re: Array Assignment
            Level 7
            > I'm not sure how it works in flash exactly, because flash doesn't support
            > pointers.

            It does .. sort of. Every object variable (including arrays) is really a
            reference/pointer to the actually object. Variables in Flash cannot
            directly hold actual objects themselves

            > But what is happening is that when you assign the value of one
            > variable to another only the values are assigned.

            Yes .. the value of an array variable is a reference/pointer to an array.

            > But when you try to assign
            > arrays in the way you are attempting the addresses of one array is
            > assigned
            > rather than the value.

            Yes .. effectively that is what it does

            > Try using a for loop and assign the values that way.
            [snip]

            This is called shallow copying .. depending on what is in the array (ie if
            it is simple value rather than array/object reference/pointers) then that
            may be just what you need. Its hard to be sure unless one knows what things
            live in the array and what you really want it to do.
            --
            Jeckyl


            • 3. Re: Array Assignment
              Marghoob Suleman Level 1
              hi u can use as slice method.
              var b_ary:Array = a_ary.slice()

              will work.. :-) enjoy
              • 4. Re: Array Assignment
                Level 7
                > var b_ary:Array = a_ary.slice()

                yeup. . thats probably better for doing a shallow copy than iterating
                yourself (much faster). I keep forgetting 'slice' :) The comments I made
                earlier regarding shallow copy (vs deep copy) still apply .. 'slice' may or
                may not do what you really want (and unless we know what is in the array and
                how you want the copy to work, we cannot answer that).
                --
                Jeckyl