2 Replies Latest reply on Jul 13, 2010 1:20 PM by jimmyoneshot

    Trying To Understand Some Code Which Uses the [i] Syntax

    jimmyoneshot Level 1

      Ok I'm trying to get a grip on using loops within flex and I found the following code somewhere which utilises a for in loop. It basically is a function that creates several labels for all of the elements in an object. One thing I cannot understand is what the [i] is used for on line 5 i.e. (myArray[i])  or more specifically what the purpose of such Square brackets in situations like these are used for. Does it refer to looking for all Strings in the array (as defined on line 3) as I assume it does? It's just pretty confusing. Can anyone point me to a guide on the syntax of square brackets and what they mean or help me out? with an explanation? Cheers.

       

       

      private function createLabels():void{
      
      var myArray:Array = ['one', "two", 3];
      
      for(var i:String in myArray){
      
      var myLabel:Label = new Label();
      myLabel.text = (myArray[i]);
      myBox.addChild(myLabel);
      }
      }
      
      
      
        • 1. Re: Trying To Understand Some Code Which Uses the [i] Syntax
          Solydest Level 2

          You've probably already seen these, but here are a couple of links that may help.

           

          http://help.adobe.com/en_US/FlashPlatform//reference/actionscript/3/operators.html#array_a ccess

           

          http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html

           

           

          What's basically happening is that the "for" loop goes through each item in the array.  Don't be confused by the fact that two of the items in the array are Strings and one is an Integer - variable i being a String has nothing to do with the data type of the elements in the array.

           

          The following code:

           

          private function createLabels():void
          {
               var myArray:Array = ['one', 'two', 3];
                    
               for(var i:String in myArray)
               {
                    trace("i = " + i);
                    trace(myArray[i]);
               }
          }
          

           

          gives the following results:

           

          i = 0
          one
          i = 1
          two
          i = 2
          3
          

           

          From that we can see that the "for" loop is assigning the index number of each array element, not the actual array element ('one', 'two', or 3) to the variable i.  So calling myArray[i] is the same as calling myArray[0] the first time through the loop.  This type of "for" loop is just a shorter way of doing the following, which produces exactly the same results:

           

          private function createLabels():void
          {
               var myArray:Array = ['one', 'two', 3];
                                   
               for(var i:int = 0; n < myArray.length; i++)
               {
                    trace("i = " + i);
                    trace(myArray[i]);
               }
          }
          

           

          Hope that helps.

          Cheers!

          • 2. Re: Trying To Understand Some Code Which Uses the [i] Syntax
            jimmyoneshot Level 1

            Thanks a lot, that's brilliant and is exactly what I was looking for.

             

             

            I'm just trying to brush up on a few things that I've never used before in Flex which loops was one of. Thanks again.