6 Replies Latest reply on Jul 27, 2007 8:06 AM by YevgenK

    Looping a variable name?

    Handycam Level 1
      I've got this function:

      public var choiceList0:Array = new Array();
      private function getLists():void {
      for each (var m:XML in xmlData.step.ingredientList[0].items.item.attribute("ln1")){
      var i:String = m.toString();
      choiceList0.push(i);
      }
      trace(choiceList0); }

      It works fine; choiceList0 correctly becomes an array of strings of the ln1 attributes of the first ingredientList node.

      Now I need to do this for choiceLis1, choiceList2, etc.

      I can do a for loop, as in:
      private function getLists():void {
      for (var n:Number=0; n<10; n++) {
      for each (var m:XML in xmlData.step.ingredientList[n].items.item.attribute("ln1")){
      var i:String = m.toString();
      choiceList0.push(i); <--- BUT HERE'S MY PROBLEM
      }
      }
      }

      How do I "increment" the name of the array to push each loop into? I've tried choiceLiist[n], no luck.

      Thanks!


        • 1. Re: Looping a variable name?
          AndyWW
          Without knowing more about the purpose of the choiceList arrays I would say that you have a couple of options for solving this. One is to create an "array of arrays"; the other is to create a "map of arrays". Assuming that you want to reference the different choiceLists based upon the index into the ingredientLists I would do something like:

          var choiceLists: Array = new Array();
          for (var n: int = 0; n < 10; ++n)
          {
          var choiceList: Array = new Array();
          choiceLists[n] = choiceList;
          for (var m: XML in xmlData.step.ingredientList[n].items.attribute("ln1"))
          {
          choiceList.push(m.toString());
          }
          }

          The "choiceLists" object will contain 10 arrays, each of which is a "choiceList" array. Another option would be to use a two dimensional array for the choiceLists array: the first dimension would be the choiceList number (0 through 9) and the second dimension would be the entries in the choiceList.

          I would use a map if you want to use "meaningful" names for your choiceLists. (A map is, in AS, just an Object with dynamically created properties).

          • 2. Re: Looping a variable name?
            Handycam Level 1
            I thought of something like that, thanks.

            Except your code throws an error, the all-to-familiar "1067: Implicit coercion of value of type String to an unrelated type XML".

            This was on the line :
            for (var m: XML in xmlData.step.ingredientList[n].items.attribute("ln1"))

            As I used it in a function:

            private function getLists():void {
            for (var n: int = 0; n < 10; ++n)
            {
            var choiceList: Array = new Array();
            choiceLists[n] = choiceList;
            for (var m:XML in xmlData.step.ingredientList[n].items.attribute("ln1"))
            {
            choiceList.push(m.toString());
            }
            }
            }
            • 3. Re: Looping a variable name?
              Handycam Level 1
              I think I may have it now:

              private function getLists():void {
              for (var n:int = 0; n<5; n++) {
              var tempArray:Array = new Array();
              for each (var m:XML in xmlData.step.ingredientList[n].items.item.attribute("ln1")){
              var i:String = m.toString();
              tempArray.push(i);
              }
              choiceLists.push(tempArray);
              }
              s1.choices = choiceLists[0];
              s2.choices = choiceLists[1];
              etc....
              }
              • 4. Re: Looping a variable name?
                AndyWW Level 1
                Well, yes, I typed the "for" line incorrectly ... I missed the "item" property from the "items" references. But I think you missed the point of my reply. I was suggesting that you have an array of arrays. Setting "s1.choices", "s2.choices", etc. is somewhat laborious. But I don't actually know what "s1", "s2", etc. are, and so I don't know the context. If you are content with your solution then I think that is fine!
                • 5. Re: Looping a variable name?
                  Handycam Level 1
                  No, thanks, you pointed me in the right direction. I wound up using:

                  private function getLists():void {
                  var xl:XMLList = xmlData.step.ingredientList;
                  var numLists:int = xl.length();
                  for (var n:int = 0; n<numLists; n++) {
                  var tempArray:Array = new Array();
                  for each (var m:XML in xmlData.step.ingredientList[n].items.item.attribute("ln1")){
                  var i:String = m.toString();
                  tempArray.push(i);
                  }
                  choiceLists.push(tempArray);
                  }
                  trace(choiceLists);
                  s1.choices = choiceLists[0];
                  s2.choices = choiceLists[1];
                  s3.choices = choiceLists[2];
                  s4.choices = choiceLists[3];
                  s5.choices = choiceLists[4];
                  }


                  Where the s1, s2, s3 are component instances. The component has a variable "choices".
                  • 6. Re: Looping a variable name?
                    YevgenK Level 1
                    Try this way.
                    private function clr_form():void {
                    var i:int;
                    for(i = 1; i < 11; i++){
                    this["w" + i].text = '';
                    this["n" + i].text = '';
                    this["s" + i].text = '';
                    this["c" + i].text = '';
                    }
                    }