5 Replies Latest reply: Aug 22, 2013 10:42 AM by sinious RSS

    Nested Arrays: Syntex help needed

    Charine -FB buggoop

      In my spelling game I have Levels of 10 words in each level and each word has to be broken up into its phonetic sounds. Because I'm working with phonetic sounds, I can not simply split the word up into letters - so I had to do each word manually according to its sounds (not individual letters).

       

      Here are the first five words of level 1, each split as I want it:

       

      public var  wordsL1W1:Array = ["e","l',"e","ph","a","nt" ];

      public var  wordsL1W2:Array = ["h","a","s"];

      public var  wordsL1W3:Array = ["o","f"];

      public var  wordsL1W4:Array = ["o","ff"];

      public var  wordsL1W5:Array = ["o","n"];

       

      I would like to add them all to a nested array (named wordsL1) so that I can manipulate the array as a whole e.g. loops and things. Here is how I wrote it:

       

      public var  wordsL1:Array = [wordsL1W1,wordsL1W2,wordsL1W3,wordsL1W4,wordsL1W5];

       

      And when I trace it like below I expect the following in the output box: e,l,e,ph,a,nt

       

           trace(wordsL1[0]);

       

      BUT I get //null in the output pannel

       

      What did I do wrong?

       

      Thanks

      Charine

        • 1. Re: Nested Arrays: Syntex help needed
          Ned Murphy MVP

          For the code that you show, what you expect to get is what you should get, so what you might have done wrong might lie in something you are not showing.  You might need to show more of what's involved with how you arrange this code in the class file... maybe you are calling the trace before you've defined the Arrays?

           

          Have you tried tracing wordsL1 itself to see if it indicates it is an Array?

          • 2. Re: Nested Arrays: Syntex help needed
            Charine -FB buggoop Community Member

            Hi Ned,

            Below is the code for the entire class file. All the other stuff in here works fine it is just the nested code in red that traces as null if I ask for a specific element in the array. If I trace the entire array like you asked I get a bunch of commas, I see ,,,,,,,,, in the output box.

             

            I'm sure it is just me that does not know how to write an array within a array properly. I tried to find a tutorial but all says just to put var nameOfNewArray:Array = [nameOf1stNestedArray,nameOf2ndNestedArray,...etc]

             

            package

            {

                import flash.display.MovieClip;

                import flash.text.TextField;

                import flash.events.Event;   

                import flash.utils.Timer;

                import flash.events.*;

                //import flash.events.MouseEvent;

             

                public  dynamic class WordArray extends MovieClip

                    {

                            public var wordtext:wordText =  new wordText;

                            var activeWordArray:Array;

                            public var  wordsL01:Array = ["elephant","has","of","off","on","not","got","in","is","it"];     //This was a test with just a single array, this works but will be replaced with the nestd arrays once I get them to trace correctly

                        public var  wordsL1:Array = [wordsL1W1,wordsL1W2,wordsL1W3,wordsL1W4,wordsL1W5,wordsL1W6,wordsL1W7,wordsL1W8,wordsL1W 9,wordsL1W10];

            public var  wordsL1W1:Array = ["e","l","e","ph","a","nt"];

            public var  wordsL1W2:Array = ["h","a","s"];

            public var  wordsL1W3:Array = ["o","f"];

            public var  wordsL1W4:Array = ["o","f","f"];

            public var  wordsL1W5:Array = ["o","n"];

            public var  wordsL1W6:Array = ["n","o","t"];

            public var  wordsL1W7:Array = ["g","o","t"];

            public var  wordsL1W8:Array = ["i","n"];

            public var  wordsL1W9:Array = ["i","s"];

            public var  wordsL1W10:Array = ["i","t"];

             

            // level 2 to 30 to be inserted here once level one works

             

             

                            private var tf:TextField;

                            public var letterArray:LetterArray;

                       

                            // ***constructor code

                            public function WordArray(_tf:TextField)

                            {   

                                tf = _tf;

                                nextWordF();

                             }

             

                            function nextWordF():void

                            {

                                if(wordsL01.length>0)

                                {

                                    activeWordF();

                                    populateMyWordBox();

             

                                }

                                else

                                    {

                                        //quiz is complete

                                    }

                                }

                                private function activeWordF():void

                                {

                                    activeWordArray = wordsL01.shift().split("");

                                     // this array needs to be passed to the class that checks which letters are clicked (or used in this class if that's where the clicked letters are handled) so the clicked letters can be compared to activeWordArray's elements.   

                                    // when the clicked letters are completed for this word, call nextWordF().

                                    }

             

                                    public function populateMyWordBox()

                                    {

             

                                        trace(wordsL1[0]);         //gives null in output box in stead of  e,l,e,ph,a,nt           

                                        trace(wordsL01[0]);

                                        tf.text = activeWordArray.join("");

                                    }

                            }

                    }

            • 3. Re: Nested Arrays: Syntex help needed
              sinious MVP

              You're assigning an array full of objects you haven't even created yet. The compiler creates them in the order in which you write.

               

              Simply move this line below all the defined variables it uses. e.g.:

               

              package

              {

                     import flash.display.Sprite;

               

                     public class classarray extends Sprite

                     {

                           public var  wordsL1W1:Array = ["e","l","e","ph","a","nt"];

                           public var  wordsL1W2:Array = ["h","a","s"];

                           public var  wordsL1W3:Array = ["o","f"];

                           public var  wordsL1W4:Array = ["o","f","f"];

                           public var  wordsL1W5:Array = ["o","n"];

                           public var  wordsL1W6:Array = ["n","o","t"];

                           public var  wordsL1W7:Array = ["g","o","t"];

                           public var  wordsL1W8:Array = ["i","n"];

                           public var  wordsL1W9:Array = ["i","s"];

                           public var  wordsL1W10:Array = ["i","t"];

               

                           // now that all vars are defined/exist

                           public var  wordsL1:Array = [wordsL1W1,wordsL1W2,wordsL1W3,wordsL1W4,wordsL1W5,wordsL1W6,wordsL1W7,wordsL1W8,wordsL1W 9,wordsL1W10];

               

                          public function classarray()

                          {

                                  trace(wordsL1[0]);

                          }

                   }

              }

               

              Traces:

               

              e,l,e,ph,a,nt

              [SWF] classarray.swf - 2,083 bytes after decompression

              [Unload SWF] classarray.swf

              • 4. Re: Nested Arrays: Syntex help needed
                Charine -FB buggoop Community Member

                Thanks sinious,

                Simple fix for a mistake due to inexperience. Wouldn't do that again LOL

                • 5. Re: Nested Arrays: Syntex help needed
                  sinious MVP

                  Some look ahead compilers would fix this issue for you but in this case we all learn something new every day . You're welcome and good luck!