7 Replies Latest reply on Aug 14, 2007 4:01 PM by nik c

    mental block with ARRAYS

    nik c

      I got to loop through one array and selectively transfer values from it into another array.

      contentItems[ i ].attributes.id != "x" --- looks at an id attribute in an XML sheet and if this is x the respective data is not transferred from one to the other array.

      As you can see the source data is from an XML file (.attributes.title, etc.).

      i is the counter for the source array and k is the counter for the destination array. I am using different ones, as I want the indexes of the destination array to be continuous, whereas I found if I use i for this as well, I end up with missing index numbers.

      I am having a complete mental block at this moment in time - I am sure I am not seeing the blinding obvious of why this does not work. Tracing 'contentArr' in any shape or form after the transfer supposedly should take place (ie further down the script) simply reports an 'undefined'


      Nik C

        • 1. Re: mental block with ARRAYS
          CaioToOn! Level 3
          Hi, there.

          I don't know if I understood well, but:

          First of all, is the code below all the code you have? Because you need to declare de array object, just like:

          var k:Number = 0;
          var vidArr:Array = new Array();
          var contentArray:Array = new Array();

          Second point, at the line "var vidArr:Array = new Array;" you forgot the parentesis [new Array()]

          And third one, if contentItems is a XML object, so you need to put contentItems.childNodes.length instead of contentItem.length.

          Did I got you doubt?

          • 2. Re: mental block with ARRAYS
            FlashTastic Level 1
            You're also re-defining vidArr in each loop, so therefore the length of it will always be one.
            • 3. mental block with ARRAYS
              nik c Level 1

              Thanks for the replies. Just realised the thing about the wrong place for the vidArray definition. Below is the complete function and it still doesn't work yet.

              I am leaving the // trace(etc.) in there as you can see up top which point everything works (or at least traces). It all seems to fall apart at the ...

              contentArr[k][j][0] = vidArr[ I ][ j ].attributes.title;

              .... and i can't get my head 'round it.


              Nik C

              • 4. mental block with ARRAYS
                Greg Dove Level 4
                I can't test it because I don't have the xml, but I would probably try to do it like this. Assumes contentArr is declared somewhere as a new Array();
                • 5. Re: mental block with ARRAYS
                  nik c Level 1

                  That is what I have been trying to do!

                  And... I have just been working on a .push solution myself.

                  But I have never seen this syntax before:


                  var newContentIndex = contentArr.push([])-1;

                  how does that work? You are setting the newContentIndex to the length of contentArr -1 to get it to start from 0 by pushing an empty list onto the array? is that right?

                  How dos it differ from e.g.
                  var k:Number=0;

                  and then
                  inside the 'if'?

                  Thanks again,

                  Nik C
                  • 6. Re: mental block with ARRAYS
                    Greg Dove Level 4
                    You're welcome.
                    The push method just adds a new element to the end of the array and returns the new length of the array, so you don't need to use k when you use push.
                    What I did there wasn't very easy to read sorry, I just did it quickly. Both of these are the same:

                    var newContentIndex = contentArr.push(new Array())-1;
                    var newContentIndex = contentArr.push([])-1;

                    This puts a new empty array as a new element at the end of your contentArr array and gives us its index.

                    Then I pushed new elements into that new array which are themselves arrays of the three attributes you're copying. It gets kinda complicated to describe.lol

                    If you do this type of thing again or often you may want to check out the XPathAPI - I'm pretty sure you could use the xml itself rather than converting it.

                    BTW I think you were missing out the new Array() or [] steps last time for each new j copy copy you were making, which is why it wasn't working.
                    • 7. Re: mental block with ARRAYS
                      nik c Level 1

                      It wasn't really a problem to read but I hadn't seen that syntax before and just wanted to confirm that I understood it correctly! That XPathAPI looks like a worthwhile thing for me to look into, as we will be doing a fair bit of XML stuff in the near future!

                      The mental block is slowly dissolving, thanks for the aspirin!

                      Nik C