4 Replies Latest reply on Jul 21, 2007 11:27 AM by AndyWW

    How to create a loop?

    Handycam Level 1
      I have this function:

      private function setUpHeaders():void {
      sLabel1 = xmlData.step1.ing..title+" - "+xmlData.step1..amt+":";
      sLabel2 = xmlData.step2.ing..title+" - "+xmlData.step2..amt+":";
      sLabel3 = xmlData.step3.ing..title+" - "+xmlData.step3..amt+":";
      sLabel7 = xmlData.step7.ing..title+" - "+xmlData.step7..amt+":";


      It lloks like an excellent candidate for a loop. However, the attempts I made at a "for" loop failed. How would you folks make a loop here?
        • 1. Re: How to create a loop?
          Same syntax as all for loops: for(declaration; condition; execution){}

          Or, more specifically:
          for(var myVar:Number = 0; myVar < 2; myVar++)
          • 2. Re: How to create a loop?
            Handycam Level 1
            Thanks, that part I've got. I guess I should've been more specific...

            How to use the looping var (myVar) to the objects in question:

            for(var myVar:Number = 0; myVar < 2; myVar++) {
            sLabel[myVar] = xmlData.step[myVar].ing..title+" - "+xmlData.step[myVar]..amt+":";

            does not work. I have tried parens instead of brackets too. Anyone?
            • 3. Re: How to create a loop?
              ZClaes Level 1
              You can't treat XML data exactly like an array. See the documentation on the XML class for more complete help.
              For iterating children, I usually do something like:

              for each(var child_node:XML in parent.children())
              var name:String = child_node.@name;
              var data:String = child_node.toString();

              Hope that helps a bit.
              • 4. Re: How to create a loop?
                AndyWW Level 1
                I am not sure of the class of the xmlData object, and I am also uncertain of what the "..title" and "..amt" notation means, but in general it is possible to use the array syntax in AS for properties. I would try:

                for (var i: int = 1; i < 8; ++i)
                this["sLabel" + i] = xmlData["step" + i]..title + "-" + xmlData["step" + i]..amt + ";";

                You may need to cast the properties from the xmlData object in order to apply the "..title" and "..amt" accessors.

                You can access properties in any Object using brackets and a string within the brackets.