13 Replies Latest reply on Jan 12, 2007 9:37 AM by mxc

    Scope issue

    aniebel Level 2
      How do I get this loop to fill an array outside of itself? I want aPicNames to consist of an attribute from aItems.
        • 1. Re: Scope issue
          mxc Level 1
          As far as I can see your array should work fine. The problem must be somewhere else in the code.
          For example the loop will not execute unless the function is called, success == true and aItems.length is greater than 0.

          I have attached a modified version of your code so you can see the array loop is working.
          • 2. Re: Scope issue
            aniebel Level 2
            Thanks for replying. Yes, the loop is working but if you trace(aPicNames) outside the loop, you get undefined. I need to be able to use that array outside the function.

            • 3. Re: Scope issue
              mxc Level 1
              It also works outside the function. The following code traces after the function, you can see the array is fully populated

              var xnRoot:XMLNode;
              var aItems:Array;
              var aPicNames:Array = new Array();
              //initialize variables
              initXML(true);

              function initXML(success:Boolean):Void {
              if (success == true) {
              xnRoot = bhr_xml.firstChild;
              aItems = xnRoot.childNodes;
              //loop through Array
              for (i=0; i<5; i++) {
              currImage = aItems .attributes.image;
              aPicNames.push('hi');

              }
              }
              }
              trace(aPicNames)
              • 4. Re: Scope issue
                aniebel Level 2
                hmmm... not sure why it's not working on my end. I'll look into it further. Thanks!
                • 5. Re: Scope issue
                  mxc Level 1
                  glad to help ;)

                  I would trace all the other variables and add an if function to check what happens if success == false;

                  regards
                  Michael
                  • 6. Re: Scope issue
                    aniebel Level 2
                    The XML is loading. I've done that trace already. Somewhere along the line the attributes from aItems is getting lost. If I trace currImage INSIDE the loop, it lists all the attributes (which are image file names). But outside the initXML function, I get "undefined". And since I am creating aPicNames from that list of attributes, I am getting the same. Just wondering if I'm going about it the wrong way.
                    • 7. Re: Scope issue
                      mxc Level 1
                      Yes, try

                      this[currImage+i] or

                      _parent[currImage+i]
                      • 8. Re: Scope issue
                        aniebel Level 2
                        Sorry, where do I put that?
                        • 9. Re: Scope issue
                          mxc Level 1
                          oops sorry ;) inside the loop as follows

                          for (var i = 0; i<aItems.length; i++) {
                          this[currImage+i] = aItems .attributes.image;
                          aPicNames.push(this[currImage+i] );
                          }
                          • 10. Re: Scope issue
                            aniebel Level 2
                            no cigar... now I get "undefined" for each loop iteration in the output panel when tracing "aPicNames" just outside the loop but inside the function. If I trace the same thing inside the loop, I get undefined for each iteration x array length.
                            • 11. Re: Scope issue
                              mxc Level 1
                              Ok, its sort of tough for me to debug without the xml but I will try another guess.

                              Are you passing a string as currImage from aItems.attributes.image ?
                              Are the images written as eg ('image1', 'image2') etc?

                              I think that you will find that if you pass a string the code will work out.

                              Below is my test version as a demo

                              var xnRoot:XMLNode;
                              var aItems:Array = new Array('image1','image2','image3');
                              var aPicNames:Array = new Array();
                              //initialize variables
                              initXML(true);

                              function initXML(success:Boolean):Void {
                              if (success == true) {
                              xnRoot = bhr_xml.firstChild;
                              //aItems = xnRoot.childNodes;
                              //loop through Array
                              for (i=0; i<5; i++) {
                              currImage.image = aItems ;
                              aPicNames.push(currImage.image);

                              }
                              }
                              }

                              trace(aItems)
                              • 12. Re: Scope issue
                                aniebel Level 2
                                Sorry, I just tested by using a button event handler to trace the aPicNames and that worked. I just need to figure out now how to get the rest of the code to recognize it. I'll post my results when I get it working. THANKS!!!
                                • 13. Re: Scope issue
                                  mxc Level 1
                                  No problem. Best of luck with it ;)