30 Replies Latest reply on Nov 30, 2006 2:58 AM by Jude86_7

    Arrays

    Jude86_7
      Please help i have read through the previous post about this subject however i have applied this to my problem and it's not working wonder if anyone can help??

      I have an array at the moment, where the content is determined by what the user inputs, however if the user enters data in a certain order it means that 'undefined' appears in the array, which i need to take out. so to do this i used the following code:

      function removeUndefined(aone:Array):Array{
      for(var i=0; i<aone.length; i++){
      if(aone == undefined || aone == "undefined"){
      aone.splice(I,1);
      }
      }
      return aone;
      }
      trace(aone);
      trace(removeUndefined(aone));

      the two traces that come back are exactly the same, both with the undefined values in. Where am i going wrong??

      Please help
        • 1. Re: Arrays
          arunbe Level 1
          Try out like this !
          • 2. Re: Arrays
            Jude86_7 Level 1
            Thank you for your prompt reply, this works great for the values that i entered e.g.
            aone = [1, undefined, 3, 4, 5, undefined];
            however i dont know what is going to be in the array every time as the content of the array depends on the user input. therefore i dont know what to put in the square brackets.
            Is there anyway i can do this?

            Cheers
            • 3. Re: Arrays
              kglad Adobe Community Professional & MVP
              define your array however you wish and call removeUndefined() whenever you wish.
              • 4. Re: Arrays
                Jude86_7 Level 1
                Can anyone help me??

                is it even possible for me to do what i am asking above??

                what do i put in the brackets??

                this is driving me crazy please help!!!!!
                • 5. Re: Arrays
                  Jude86_7 Level 1
                  how do you mean define my array however i wish??
                  • 6. Re: Arrays
                    kglad Adobe Community Professional & MVP
                    just what i said: define aone to suit your needs.

                    arunbe just used an example aone that you do not need to use.
                    • 7. Re: Arrays
                      Devendran
                      No need to Worry about it.

                      Just Create an array using this format

                      var aone:Array = new Array();

                      This will create Your Empty Array. When ever u wish Push it into your aone array or assign it.

                      aone = myArrayContent;
                      or
                      aone.push(newValue);
                      Both method will assign array value.
                      then call the function. removeUndefined();
                      • 8. Re: Arrays
                        Jude86_7 Level 1
                        thank you for your replies........im nearly there im sure i am, ive been working on this action script stuff for so long its all getting muddled lol.

                        i have put this code, but the traces both come back undefined where have i gone wrong??

                        var aone:Array=newArray();
                        function removeUndefined(aone:Array):Array {
                        for (var i = 0; i<aone.length; i++) {
                        if (aone == undefined || aone == "undefined") {
                        aone.splice(i, 1);
                        }
                        }
                        return aone;
                        }
                        var mydata:LoadVars = new LoadVars();
                        mydata.load("answer.txt");
                        mydata.onLoad = function(ok) {
                        _global.aone[0] = aone.push(mydata.QuestionOneA);
                        removeUndefined();
                        trace(aone);
                        trace(removeUndefined(aone));
                        }

                        cheers
                        • 9. Re: Arrays
                          Peter Lorent Level 2
                          This line:
                          var aone:Array=newArray();
                          misses a space between the keyword new and Array
                          var aone:Array=new Array();
                          • 10. Re: Arrays
                            Jude86_7 Level 1
                            unfortunatley its still not working............but well spotted tho
                            • 11. Re: Arrays
                              Devendran Level 1
                              Hi
                              In what format the Variable "QuestionOneA" will be?
                              if it is a single string no need of this much of code.

                              • 12. Re: Arrays
                                Jude86_7 Level 1
                                where do i need to call the removeUndefined()???
                                • 13. Re: Arrays
                                  Devendran Level 1
                                  when u call that function u have to send the array as argument.

                                  after assigning the Value you can call it anytime
                                  • 14. Re: Arrays
                                    Jude86_7 Level 1
                                    you have to send the array as arguement??

                                    and i need the _global thingy in i think because there isnt just one string in that variable there are two i.e.

                                    _global.aone[0] = mydata.QuestionOneA;
                                    _global.aone[1] = mydata.QuestionTwoA;

                                    i have put this code into it instead:

                                    .....
                                    mydata.onLoad = function(){
                                    aone = new Array();
                                    _global.aone[1] = aone.push(this.QuestionTwoA);

                                    and i have put the code:

                                    removeUndefined();
                                    into the part of code on the mc where the variable is put into the text boxes

                                    the result is '1' and '2' in the output boxes once i have exported the movie

                                    mmm weird???
                                    • 15. Re: Arrays
                                      Jude86_7 Level 1
                                      the code i said i put in it was actually:

                                      ......
                                      mydata.onLoad=function(){
                                      aone=new Array();
                                      _global.aone[0]=aone.push(this.QuestionOneA);
                                      _global.aone[1]=aone.push(this.QuestionTwoA);

                                      ...........
                                      • 16. Re: Arrays
                                        Devendran Level 1
                                        Here s the Answer.
                                        • 17. Re: Arrays
                                          Devendran Level 1
                                          I hope this will help you solve your problem
                                          • 18. Re: Arrays
                                            Peter Lorent Level 2
                                            Won't work. The elements in the array are defined... some of them are just empty.
                                            • 19. Re: Arrays
                                              Jude86_7 Level 1
                                              how do you mean??? .........youre right tho i cant get it to work........any suggestions??
                                              • 20. Re: Arrays
                                                Peter Lorent Level 2
                                                Try attached.

                                                • 21. Re: Arrays
                                                  Peter Lorent Level 2
                                                  BTW: I named the text to load 'data.txt' so you should rename that to 'answer.txt'.
                                                  • 22. Re: Arrays
                                                    Jude86_7 Level 1
                                                    how do i get around this point where elements in the array are defined and some are just empty.........is there a way??
                                                    • 23. Re: Arrays
                                                      Jude86_7 Level 1
                                                      sorry i didnt see your reply lol

                                                      i am just on my way home from work now but i will try that code when i get in, many thanks

                                                      fingers crossed i can get it working

                                                      thank you again :)
                                                      • 24. Re: Arrays
                                                        Peter Lorent Level 2
                                                        I load the following text:
                                                        QuestionOneA=answ1,,answ3,answ4,&QuestionTwoA=answ1,answ2,,answ4&

                                                        In QuestionOneA answ2 and answ5 are empty, in QuestionTwoA answ3 is empty.
                                                        The code I provided takes out these empty values and returns:
                                                        for QuestionOneA: answ1,answ3,answ4
                                                        for QuestionTwoA: answ1,answ2,answ4

                                                        • 25. Re: Arrays
                                                          Jude86_7 Level 1
                                                          hello,

                                                          your code works beautifully i even managed to add a line in where it took out any 'undefined' strings too.

                                                          however, if there is more than one 'undefined' in a row in the array it only deletes one of them??

                                                          so if this is in the text file:
                                                          &QuestionOneA=hello,&QuestionTwoA=there,undefined,undefined,undefined

                                                          then the code returns:
                                                          hello,there,undefined

                                                          ???

                                                          very strange, i have attached my code below is there any glaring problems??
                                                          maybe something to do with the amount of times the array loops??

                                                          function removeUndefined(inp:String):Array{
                                                          var temp_array:Array = new Array();
                                                          temp_array = inp.split(",");
                                                          for(var i=0; i<temp_array.length; i++){
                                                          if(temp_array == undefined || temp_array == "undefined"){
                                                          temp_array.splice(i,1);
                                                          }
                                                          if(temp_array == ""){
                                                          temp_array.splice(i,1);
                                                          }
                                                          }
                                                          return temp_array;
                                                          }
                                                          ........

                                                          thanks
                                                          • 26. Re: Arrays
                                                            Marghoob Suleman Level 1
                                                            Hi Jude,

                                                            Please see the attach code.

                                                            Is this what you are looking for ?

                                                            • 27. Re: Arrays
                                                              Jude86_7 Level 1
                                                              mint!!!! thats great

                                                              it works beautifully
                                                              (only i put split(",") in instead of splice();)

                                                              now all i need to do is get rid of the commas

                                                              cheers for your help,

                                                              much appreciated
                                                              • 28. Re: Arrays
                                                                shree_shiralkar
                                                                I think u need to check your code once again...cause in the last forum you copied your code and it's like this....

                                                                i have put this code, but the traces both come back undefined where have i gone wrong??

                                                                var aone:Array=newArray();
                                                                function removeUndefined(aone:Array):Array {
                                                                for (var i = 0; i<aone.length; i++) {
                                                                if (aone == undefined || aone == "undefined") {
                                                                aone.splice(i, 1);
                                                                }
                                                                }
                                                                return aone;
                                                                }
                                                                var mydata:LoadVars = new LoadVars();
                                                                mydata.load("answer.txt");
                                                                mydata.onLoad = function(ok) {
                                                                _global.aone[0] = aone.push(mydata.QuestionOneA);
                                                                removeUndefined();
                                                                trace(aone);
                                                                trace(removeUndefined(aone));
                                                                }

                                                                In the above code the "if" condition statement -
                                                                if (aone == undefined || aone == "undefined") {
                                                                is totally wrong
                                                                instead write the following code....
                                                                if (aone == undefined || aone == "undefined") {

                                                                then check it again....please

                                                                thanks and regards,
                                                                shreeram
                                                                • 29. Re: Arrays
                                                                  Peter Lorent Level 2
                                                                  Then the code would be:

                                                                  • 30. Re: Arrays
                                                                    Jude86_7 Level 1
                                                                    i am slightly confused.............however because both of those if statements look the same to me, however i could not get that code working properly so i used the following............try this it might help:

                                                                    .....
                                                                    function removeUndefined(inp:String):Array{
                                                                    var s = inp.split(",");
                                                                    var temp_array:Array = new Array();
                                                                    for (var i=0; i<s.length; i++){
                                                                    if(String(s ) != "undefined"){
                                                                    temp_array.push(s
                                                                    );
                                                                    }
                                                                    }
                                                                    return temp_array;
                                                                    }
                                                                    var mydata:LoadVars = new LoadVars();
                                                                    mydata.load("answer.txt");
                                                                    mydata.onLoad = function(ok:Boolean){
                                                                    if(ok){
                                                                    _global.aone[0] = removeUndefined(mydata.QuestionOneA);
                                                                    ........

                                                                    hope this helps