5 Replies Latest reply on Jun 12, 2009 5:02 PM by *Prashant Shelke*

    Array of cats, dogs, and birds

    laxmidi Level 1

      Hi,

       

      I have an array and I need to find the number of items in each category. For example:

       

      myArray = ["bird", "bird", "bird", "cat", "cat", "dog", "dog", "dog", "dog"];

       

      What's the best way to determine that there are 3 birds, 2 cats, and 4 dogs?

       

      Thank you.

       

      -Laxmidi

        • 1. Re: Array of cats, dogs, and birds
          atta707 Level 2

          best way would be to iterate the array only once and in each iteration increment a counter for each type in the array.

          1 person found this helpful
          • 2. Re: Array of cats, dogs, and birds
            leybniz Level 4

            Performance wise you should build your own Array class descendant incapsulating this kind of functionality,

            for example the skeleton for that class could be:

            package {
                 
                 import flash.utils.Dictionary;     
                 
                 public class PetsArray extends Array {
                      
                      public var kinds:Dictionary;
                      
                      function PetsArray() {
                      }
            
                      protected function countUp():void {
                           kinds = new Dictionary();
                           
                           for each (var item:* in this)
                                if (kinds[item])
                                     kinds[item]++;
                                else
                                     kinds[item] = 1;     
                      }
                      
                      public function toString():String {
                           countUp();
                           var result:Array = [];               
                           
                           for (var o:* in kinds)
                                result.push(String(o) + ' : ' + kinds[o]);  
                           
                           return result.join(',');     
                      } 
                      
                 }
            }
            
            
            
            
            var p:PetsArray = new PetsArray();
            p.push("bird");
            p.push("bird");
            p.push("bird");
            p.push("cat");
            p.push("cat");
            p.push("cat");
            p.push("cat");
            p.push("dog");
            trace(p.toString());
            
            
            1 person found this helpful
            • 3. Re: Array of cats, dogs, and birds
              *Prashant Shelke* Level 4

              Iteration over the array is the only way seems to be suitable here

              • 4. Re: Array of cats, dogs, and birds
                laxmidi Level 1

                Thank you atta707, Alexander Mazuruk, and Prashant Shelke. Alexander put a lot of effort into the question, which is much appreciated.

                 

                I found a script at: http://englishblog.flepstudio.org/tutorials/removing-duplicate-elements-of-array-with-acti onscript-3/ which iterates the array.

                 

                Again, thank you for your help.

                 

                -Laxmidi

                • 5. Re: Array of cats, dogs, and birds
                  *Prashant Shelke* Level 4

                  Your always wel-come