5 Replies Latest reply on Jun 6, 2007 5:25 PM by SmileKZS

    Trouble copying array data

    SurgicalSteel Level 1
      Hey all,
      Ok, so what I'm doing (or trying to do) should be simple, I am reading some values in from a text file into an array (temp), and than iterating through that array, converting the values from String to Number, than pushing them onto a different array (scaleArray). Here's the relevant code:
      ////////////////////////////////////////////////
      var tmp:Number;
      for(var a:Number = 0; a < temp.length; a++) {
      tmp = Number(temp[a]);
      scaleArray.push(tmp);
      }
      /////////////////////////////////////////////
      but when I trace out scaleArray all I get is "undefined", and when I trace out temp and tmp I get the correct values. I can post the whole loading function if that code isn't enough. Thanks in advance.
        • 1. Re: Trouble copying array data
          kglad Adobe Community Professional & MVP
          where have you declared scaleArray to be an array?
          • 2. Re: Trouble copying array data
            Rothrock Level 5
            So if you put the code like this:

            tmp=Number(temp[a]);
            trace("tmp is "+tmp);
            scaleArray.push(tmp);

            You see the correct numbers go by but later scaleArray is undefined?

            I'm also wondering about why you would do it this way? When you make your temp array why don't you change things to numbers then?

            You might want to try parseInt() or parseFloat() instead of casting the Number. Just a thought.

            And finally I'm thinking you don't know this cool trick. You do know that arrays are copied by reference, right? So that if you do this:

            var first:Array=new Array(1,2,3,4);
            var second:Array=first;
            trace(first[0]); //<--traces 1;
            second[0]=5;
            trace(first[0]); //<-traces 5;

            Because the second array is just a reference to the first so if you change the second the first changes or if you change the first a second changes. The neat trick is that you can "clone" (make an independent copy) of the array like this:

            var first:Array=new Array(1,2,3,4);
            var second:Array=first.slice();

            I don't know for certain, but I have the feeling that might come in handy for what you are doing.
            • 3. Re: Trouble copying array data
              SurgicalSteel Level 1
              thanks for the help, i tried the slicing and parseInt and it works now, if you're curious here is how i re did it:

              var tmp:Array = new Array();
              for(var a:Number = 0; a < temp.length; a++) {
              tmp[a] = parseInt(temp[a]);
              trace(temp[a] + ":" + tmp[a]);
              }
              scaleArray = tmp.slice();
              trace(scaleArray);

              and everything seems to work fine. You asked why I don't change the strings to numbers when I make the temp array though, how would i do that? is there a way to change an entire array of strings to numbers without iterating? thanks again for the help.
              • 4. Re: Trouble copying array data
                kglad Adobe Community Professional & MVP
                no, your previous code was fine. you just needed to declare scaleArray to be an array.
                • 5. Re: Trouble copying array data
                  SmileKZS
                  yeah. The ever-useful Array.slice()
                  If you want an array which is always the same as the original array, no matter which array you are operating, use new_array=old_array;
                  If you want an array, which is a copy(like you described) just use new_array=old_array.slice();