12 Replies Latest reply on Apr 28, 2010 6:15 AM by alice_data

    Finding Index of Associative Array Elements

    alice_data Level 1

      Hi,

        I am writing to ask if there is anyone on the list tried to find out the index of elements of an associative array.

       

      public function urlLoader_complete2(e:Event):void {  
          xml2 = XML(e.target.data)..region;     
          regions = [];    
          education = [];
                     
         for (var i:int,n:int = xml2.length(); i < n; i++) {     
                                 education.push(xml2[i].@education);       
         regions[xml2[i].@name] = education;    
                             
            }       
         }

      See, these are all part of regions array, which I believe is an associative one by the way I constructed it. I tried to use indexOf to find out the index of the individual elements, but don't seem to have too much luck.

       

        for (var s:String in regions)  trace("\n" + indexOf(s));

      Right now, this does not bring me a thing. Have I done something wrong here?

      What I am trying to accomplish here is possible, right?

        • 1. Re: Finding Index of Associative Array Elements
          asafdsadfasddfsas Level 1

          Hi Alice,

           

          Hope the following example helps.

           

          var associativeArray:Array = new Array();
          associativeArray["a"] = "Hi!!";
          associativeArray["b"] = "Hello!!";
          trace(associativeArray["a"], associativeArray["b"]);

          output: Hi!! Hello!!

           

          Thanks,

          Sathish.

          • 2. Re: Finding Index of Associative Array Elements
            alice_data Level 1

            Nope, I wanted to find out that index of the associative array, like 1, 2 or 3 depending on which element it is referred. I tried using "indexOf", but that doesn't seem to be doing any good.

             

            I hope this makes sense what I am trying to do here.

            Thanks for your help.

             

            Alice

            • 3. Re: Finding Index of Associative Array Elements
              asafdsadfasddfsas Level 1

              Hope this is what you are looking for.

               

              var associativeArray:Array = new Array();
              associativeArray["a"] = "Hi!!";
              associativeArray["b"] = "Hello!!";  
              for (var index:String in associativeArray)
              {
                  trace(index+ ": " + associativeArray[index]);
              }

               

              Output:
              a: Hi!!
              b: Hello!!

               

              Thanks,

              Sathish.

              • 4. Re: Finding Index of Associative Array Elements
                alice_data Level 1

                No, that is not what I was looking for, sorry.

                Based on the example you sent, I was hoping if the output would be:

                 

                a: 0
                b: 1

                 

                Is there a function that would make this possible?

                 

                Thanks for your help.

                 

                Alice

                • 5. Re: Finding Index of Associative Array Elements
                  ShankarFlex3 Level 2

                  Hi,

                   

                  I hope this is what you are looking for http://livedocs.adobe.com/flex/3/langref/index.html , navigate to indexOf and look at the example. The problem could be with the data type.

                   

                  Also look at http://livedocs.adobe.com/flex/3/langref/operators.html#strict_equality

                   

                   

                  Regards,

                  Shankar.

                  • 6. Re: Finding Index of Associative Array Elements
                    alice_data Level 1

                    Yes, I was looking through something like that this afternoon earlier,

                    http://livedocs.adobe.com/flex/3/langref/Array.html#indexOf%28%29, and I am not sure if the array it was mentioning does not count associative arrays.

                     

                    You see, I have two separate url requests that I try to each get some information from, I am trying to create some different colors base on the ranges of data from my input file, which is in XML format. However, the number of the elements are the same, so I am trying to see if I could get the index of one, so I can use that to determine the color by using other functions.

                     

                    The problem is, the array? I found that using Flex in this context, using object and array does not make a difference at all, as long as I claim something like

                     

                      array[name] = something;

                     

                    and not

                     

                      array[0] = "Hello";

                     

                    Yet, the problem is that I cannot get the index this way. With the example based on the reference guide, does this mean that I cannot do indexOf? It looks like it does not accept associative arrays to provide the index. I use Actionscript 3 in Flex, and that didn't help either.

                     

                    Could there be a work around somewhere? Thanks for your help.

                     

                    Alice

                    • 7. Re: Finding Index of Associative Array Elements
                      ShankarFlex3 Level 2

                      Hi Alice,

                       

                      It seems that you cannot get index out of Associative arrays..

                       

                      http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_4.html

                       

                      Regards,

                      Shankar.

                      1 person found this helpful
                      • 8. Re: Finding Index of Associative Array Elements
                        alice_data Level 1

                        Yes, thanks for your confirmation on that. What I cannot figure out is why Actionscript does not allow  associative arrays to operate like hash tables in PHP or other  languages.

                        I saw a tutorial, and it says that I cannot just pull the index of the  associative array, because the items is based on the named elements and  not indexes. However, is there not a single method that I can pull the  index. I was looking through something like that this afternoon earlier,
                        http://livedocs.adobe.com/flex/3/lan...#indexOf%28%29,  and I am not sure if the array it was mentioning does not count  associative arrays.

                        Here is the file of my input:

                        Code:
                        <file>
                        <region name="region1" education="10" population="4" distance="6"/>
                        <region name="region2" education="15" population="24" distance="36"/>
                        <region name="region3" education="40" population="14" distance="12"/>
                        <region name="region4" education="29" population="32" distance="17"/>
                        </file>

                        I am trying to read in the file with the url request, I am trying  to create some different colors base on the ranges of data from my input  file, which is in XML format. However, the number of the elements are  the same, so I am trying to see if I could get the index of one, so I  can use that to determine the color by using other functions.

                         

                        If I store all the values of the different attributes in a separate  array,

                         

                         

                        ActionScript Code:
                        public function urlLoader_complete2(e:Event):void {  
                                 xml2 = XML(e.target.data)..region;       
                                 regions = new Object();          
                                 education = [];
                        population = [];
                        distance = [];
                        name = [];                      
                                 for (var i:int,n:int = xml2.length(); i < n; i++) {        
                                                   education.push(xml2[i].@education);
                        distance.push(xml2[i].@distance);
                        population.push(xml2[i].@population);
                        name.push(xml2[i].@name);                                      
                                 }    
                        regions.education = education;
                        regions.distance = distance;
                        regions.population = population;
                        regions.name = name;  
                              }

                        If I do it this way, then how can I work around it so that the elements  can still be accessed by the attribute? I want to do something like, if  the education attribute is greater than 10 and smaller than 15, then  color it red. Is what I am talking about here possible?

                         

                        Thanks for your help.

                        • 9. Re: Finding Index of Associative Array Elements
                          ShankarFlex3 Level 2

                          Hi Alice,

                           

                          Before me thinking of other solutions, is there a possiblity for you to apply the logic on server side code and add another attribute 'color'.

                           

                          This way its very clean..

                           

                          Regards,

                          Shankar.

                          • 10. Re: Finding Index of Associative Array Elements
                            alice_data Level 1

                            Hi,

                             

                            I hope this is what you are talking about, I am not supposed to edit the XML file, because that is something that my client wants to use.

                             

                            public function urlLoader_complete2(e:Event):void { 
                                     xml2 = XML(e.target.data)..region;      
                                     regions = new Object();         
                                     education = [];
                                     population = [];
                                     distance = [];
                                     name = [];   
                                     randomColors = [];
                                             
                                     for (var i:int,n:int = xml2.length(); i < n; i++) {       
                                          var myColor:uint = Math.round( Math.random()*0xFFFFFF);
                                          var fontColor:Stri = myColor.toString(16); 
                                                     
                                          randomColors.push("0x" + fontColor);
                                          education.push(xml2[i].@education);
                                          distance.push(xml2[i].@distance);
                                          population.push(xml2[i].@population);
                                          name.push(xml2[i].@name);                                     
                                     }   
                            regions.education = education;
                            regions.distance = distance;
                            regions.population = population;
                            regions.name = name; 
                            regions.color = randomColors;
                                  }

                             

                            Hope this is what you meant. Thanks for your help.

                             

                            Alice

                            • 11. Re: Finding Index of Associative Array Elements
                              ShankarFlex3 Level 2

                              Hi Alice,

                               

                              If you cannot workout on the server side then while iterating in the for loop, apply the logic and add the color to randomColors array.

                               

                              Very similar to your code but instead of selecting random color, select your choice.  ( You could have an static associative array for colors to use here)

                               

                              for (var i:int,n:int = xml2.length(); i < n; i++) {

                                            <apply the color logic here and select the color of your choice>

                               

                                             var myColor:uint = Math.round( Math.random()*0xFFFFFF);
                                             var fontColor:Stri = myColor.toString(16); 
                                                       
                                            randomColors.push("0x" + fontColor);
                                            education.push(xml2[i].@education);
                                            distance.push(xml2[i].@distance);
                                            population.push(xml2[i].@population);
                                            name.push(xml2[i].@name);                                     
                                       }

                               

                               

                              Regards,

                              Shankar.

                              • 12. Re: Finding Index of Associative Array Elements
                                alice_data Level 1

                                Hi,

                                 

                                  Thanks for your help, and your suggestions. Here is my revised code, and yes, it is working, with some workaround.

                                 

                                public function draw(a:Array):void{


                                component_width = [];

                                component_height= [];

                                var j:int=0;

                                for (var s:String in regions) {

                                shape  = new UIComponent();
                                gr = shape.graphics;
                                gr.lineStyle(3);


                                //Define line style

                                coords = regions[s];

                                 

                                if(a[j]<numericArray[j]< a[j+1]) {

                                gr.beginFill(randomColors[j]);

                                j++;

                                }

                                else if ((j+1) == xml2.length()) gr.beginFill(randomColors[j]);


                                gr.moveTo(coords[0],coords[1]);


                                for (var i:int = 2; i < coords.length; i += 2) gr.lineTo(coords[i],coords[i + 1]);


                                gr.lineTo(coords[0],coords[1]); //return the code back to the beginning

                                gr.endFill();


                                //Put this in if there is a color

                                canvas.addChild(shape);

                                }

                                }

                                 

                                Just out of curious, even though it is working this way, is there some way I can pull out individual elements, say like I want to print out the population value with regions[region1]? I tried to find this on the object class page, but I cannot seem to find it.

                                 

                                Thanks for your help.

                                 

                                Alice