8 Replies Latest reply on Jul 9, 2009 3:10 PM by shintashi

    question about multidimensional array

    shintashi Level 1

      Hi,

       

      I'm trying to figure out how to generate an array whose elements are each arrays to make a quick loading database of sorts.

      the main array could essentially be something like "employees" while each employee would have demographic data like age, salary, gender, etc.

      In some cases the information on some employees would be greater than others, and options created by "push" would imply the number of

      elements within each employee array would be variable until generated.

       

      I noticed the multidimensional array setup

       

      array = new array (new array("A","B"), new array("C","D"));

       

      and accessed by using

       

      array[n][m]

       

      But I get the feeling this type of array as it is currently described doesn't lend itself well to adding new elements in the way I've described.

       

      My goal is once an employee's information has been entered and saved, I can call up their info by array number like aEmployee[2], and if employee 2's name is Bob, then Bob's name would appear in the text field, along with all the other fields related to Bob's array, such as being male, 42 years old, and living in Witchita Kansas.

       

      Would I modify a multidimensional array, or do I want an array within an array, and is there a way to add "arrays" as elements?

        • 1. Re: question about multidimensional array
          kglad Adobe Community Professional & MVP

          for storing information that's not the same across all employees use an object to store employee data and use an array to store all employee objects:

           

          var eObj:Object={name:"whatever" age:"whatever ssn:"whatever"};

           


          var eA:Array = [];

           

          eA.push(eObj);

          1 person found this helpful
          • 2. Re: question about multidimensional array
            shintashi Level 1

            being terrible with objects, I began working on a model and this is what I came up with

            (for sake of reference namaewa = height, shinchyou = height, omosa = weight)

             

            ----

            shelley = {namaewa: "Shelley", shinchyou: 68, omosa: 132, sex: "F"};

             

            //if(1 == 1){shelley.takai = 56;}

             

            //trace(shelley.namaewa +" "+ shelley.sex);
            //trace(shelley.shinchyou);
            //trace(shelley.omosa);

             


            var peopleA:Array =[];
            peopleA[0] = shelley;

             

            trace(peopleA[0]);

            ----

             

            Most of it works fine, however

            the last line: trace(peopleA[0]);

             

            spits out [object Object]

            and I have no clue what that means.
            How would I access the material in element 0 (or any other element) so I could display name, height, weight, etc. in dynamic textfields?

             

            it is my understanding that an object is a container filled with a number of variables and/or functions, with those variables addressed by the

            objectname.variabletype and functions called by typing objectname.function();

             

             

            ----

            The following experiment rendered several textfields "undefined" but it may explain what I was trying to do:

             

            function createfile(person){
                    person = {namaewa: "John Doe", shinchyou: 72, omosa: 180, sex: "M"}
                    }

             

                newfile.onRelease = function(){
                    createfile(john);
                filename.text = person.namaewa;
                fileht.text = person.shinchyou;
                filewt.text = person.omosa;
                filesex.text = person.sex;
                   
                }

            • 3. Re: question about multidimensional array
              kglad Adobe Community Professional & MVP

              to list all the data in peopleA[0]:

               

              for(var s:String in peopleA[0]){

              trace(s+": "+peopleA[0][s]);

              }

               

              to reference namaewa of peopleA[0]:

               

              peopleA[0].namaewa

              • 4. Re: question about multidimensional array
                shintashi Level 1

                I wrote this up this afternoon - the closest I've gotten to getting the thing to work;

                 


                ----

                function display(oldnamu){
                filename.text = oldnamu.namaewa;
                fileht.text = oldnamu.shinchyou;
                filewt.text = oldnamu.omosa;
                filesex.text = oldnamu.sex;
                }

                 

                shelley = {namaewa: "Shelley", shinchyou: 68, omosa: 132, sex: "F"};
                john = {namaewa: "John Carpenter", shinchyou: 78, omosa: 182, sex: "M"};

                 


                var aPeople:Array = new Array(1);
                aPeople[0] = shelley;
                aPeople[1] = john;

                 

                prof = 0;

                 

                nextnum.onRelease = function(){
                if(prof < aPeople.length -1){prof++;}
                filenumber.text = prof;
                display(aPeople[prof]);}
                previousnum.onRelease = function(){
                if(prof > 0){prof--;}
                filenumber.text = prof;
                display(aPeople[prof]);}

                 


                fakename = "";
                fakeheight = 0;
                fakeweight = 0;
                fakesex = "U";

                 

                onEnterFrame = function(){
                    fakename = filename.text;
                    fakeheight = fileht.text;
                    fakeweight = filewt.text;
                    fakesex = filesex.text;
                   
                }

                 


                // this stuff doesn't work
                function createfile(person){
                    person = new Object();
                        person.namaewa = fakename;
                        person.shinchyou = fakeheight;
                        person.omosa = fakeweight;
                        person.sex = fakesex;
                filename.text = person.namaewa;
                fileht.text = person.shinchyou;
                filewt.text = person.omosa;
                filesex.text = person.sex;
                aPeople.push(person);
                }

                 

                newfile.onRelease = function(){
                        namu = newname.text;
                        createfile(namu);
                    }

                ---

                 

                But whereas what I want is: a "new button" to open an essentially blank file that is edited by input text fields (or radio buttons) and saved to the array memory when a "save button" is pressed,

                 

                Instead...

                this allows me to immediately edit whatever file I have open, changing the name, etc., and when I click the "new button" it saves my changes as a new file than can be viewed/displayed by sorting through the array. All my attempts at a "save changes" button have failed.

                 

                 

                is there a way to fix this?

                • 5. Re: question about multidimensional array
                  kglad Adobe Community Professional & MVP

                  create a movieclip with the input textfields (say nameTF, weightTF, heightTF, sexTF etc) you want and assign it a linkage id (say inputID).  you can then use:

                   

                  var tl:MovieClip=this;

                   

                  newBtn.onRelease=function(){

                  var mc:MovieClip=tl.attachMovie("inputID","inputMC",tl.getNextHighestDepth());  //<-if you're using any components this may be problematic

                  }

                   

                  saveBtn.onRelease=function(){

                  var mc:MovieClip=tl.inputMC;

                  var obj:Object={namaewa:mc.nameTF.text, sinchyou:mc.weightTF.text, omosa:mc.heightTF.text, sex:mc.sexTF.text};

                  aPeople.push(obj);

                  }

                  • 6. Re: question about multidimensional array
                    shintashi Level 1

                    kglad - Tested that variation and it definitely generated a popup file (one that would have to have some kind of delete function after being used). I think the real problem I'm experiencing is this:

                     

                     

                    ---

                    function editfile(person){
                            person.namaewa = fakename;
                            person.shinchyou = fakeheight;
                            person.omosa = fakeweight;
                            person.sex = fakesex;
                    }
                       
                    savefile.onRelease = function(){
                            namu = newname.text;
                            editfile(namu);
                        }  

                     

                    ---

                     

                    it doesn't work. Like If I type in the name of the object in the newname.text, such as "shelley" from my understanding, when I push the "savefile" button,

                     

                    it should read "namu" as "shelley" then run the editfile function:

                     

                            shelley.namaewa = fakename;
                            shelley.shinchyou = fakeheight;
                            shelley.omosa = fakeweight;
                            shelley.sex = fakesex;

                     

                    which If I typed values such as "Jane", "71", "137", and "F" in the input boxes

                    --

                    onEnterFrame = function(){
                        fakename = filename.text;
                        fakeheight = fileht.text;
                        fakeweight = filewt.text;
                        fakesex = filesex.text;
                       
                    }

                    ---

                    these values would replace the existing values for the shelley object. But that's not what happens. In fact, nothing happens to the shelley file at all.

                    • 7. Re: question about multidimensional array
                      kglad Adobe Community Professional & MVP

                      you have a basic misunderstanding of objects. and, your enterframe loop should be removed.  what purpose do you think it serves?

                       

                      the name of your object (like shelley) is irrelevant.  you'll never use the object's name because they're all stored in aPerson and you'll reference them by index number in aPerson.

                       

                      if you wanted to retrieve the data associated with a person with namaewu shelley, you would use the following:

                       

                      function findPerson(namaewuS:String):Object{

                      for(var i=0;i<aPerson.length;i++){

                      if(aPerson[i].namaewu==namaewuS){

                      return aPerson[i];

                      }

                      }

                      }

                       

                      look at the code i gave in my previous message.

                      • 8. Re: question about multidimensional array
                        shintashi Level 1

                        when modified the suggestion works perfectly:

                         

                        findfile.onPress = function(){
                        textname = textfieldname.text;

                         

                        textfieldnumber.text = findPeople(String(textname)).sex;
                        textfieldweight.text = findPeople(String(textname)).omosa;
                        textfieldheight.text = findPeople(String(textname)).shinchyou;
                        }

                         


                        editfile.onPress = function(){
                        textname = textfieldname.text;

                         

                        findPeople(String(textname)).sex = textfieldnumber.text;
                        findPeople(String(textname)).omosa = textfieldweight.text ;
                        findPeople(String(textname)).shinchyou = textfieldheight.text;
                        }

                         

                         

                         

                        function findPeople(namaewaS:String):Object{

                         

                        for(var i=0;i<aPeople.length;i++){

                         

                        if(aPeople[i].namaewa==namaewaS){

                         

                        return aPeople[i];

                         

                        }

                         

                        }

                         

                        }

                         

                        ------------------

                         

                         

                        one question however, from a previous suggestion for preventing duplicate array elements:

                         

                        kglad suggested this:

                        ---

                        function removeDupsF(a:Array):Array{
                        for(var i=a.length-1;i>=0;i--){
                        for(var j=i-1;j>=0;j--){
                        if(a[i]==a[j]){
                        a.splice(i,1);
                        break;
                        }
                        }
                        }
                        return a;
                        }

                        ---

                         

                        which works for most of my arrays, but it doesn't work for removeDupsF(aPeople);

                         

                        I'm going to guess this has something to do with the relationship between the objects and the arrays. Is there some modification I must make in order for it to work?