18 Replies Latest reply on Nov 30, 2006 3:09 AM by Jude86_7

    Please help - printing arrays problem

    Jude86_7
      Please help - i am currently loading data from a text file into flash using an array and loadVars......however when the data is loaded into the text box in flash there is a comma after it.......i tried using 'join()' but that didnt seem to work :( it just meant my variable was undefined. So any suggestions on how to delete commas from returned arrays would be great :)

      Thanks

      Jude
        • 2. Re: Please help - printing arrays problem
          Jude86_7 Level 1
          hello,

          ive tried that method but it doesnt seem to be working??

          is there any similar method to using the join?? the join works for me if i am loading the whole array into one text box, ie. oneAtext=_global.array.join();

          however if i just want to enter the first element in the text box ie. oneA.text = _global.array[0].join(); it doesnt work, simply displays undefined. ???

          help!!!
          • 3. Re: Please help - printing arrays problem
            arunbe Level 1
            Look, the Join Function is entirely for different puropose other than where you are using now. Its actuly add the elements of an array with the specified character. So, the only way is to use the text in the array is what i hav sent already ......
            • 4. Re: Please help - printing arrays problem
              Jude86_7 Level 1
              okay thanks for your help, but i copied it exactly and it didnt work.

              so i will look elsewhere for help.
              • 5. Re: Please help - printing arrays problem
                arunbe Level 1
                If possible can you send us the code ????
                • 6. Re: Please help - printing arrays problem
                  Jude86_7 Level 1
                  i was also trying to remove undefined elements from the array as well, so i looked on the forum for help and found a way to splice the array to remove the undefined elements, so i also applied the same theory to take the commas out. however neither work yet but i do need to take a closer look at them, but i wonder if you could take a look at the code and let me know if there are any glaring problems that i am missing?

                  the code to remove the commas from and array called 'answers':
                  function removeComma(answers:Array):Array{
                  for(var a=0; a<answers.length; a++){
                  if(answers[a] == ","){
                  answers.splice(a,1);
                  }
                  }
                  return answers;
                  }

                  code to remove undefined - this is also not working atm (on an array called 'aone'):

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

                  please help :)

                  cheers
                  • 7. Re: Please help - printing arrays problem
                    Peter Lorent Level 2
                    Show the text you are loading with LoadVars.
                    • 8. Re: Please help - printing arrays problem
                      Jude86_7 Level 1
                      this is where my code gets a little tricky really, because what i am doing is, an interactive exam paper, so the students can enter their answers into a text box, this then goes into an array (answers array) which is saved in a text file (using flash studio pro), then when the application is loaded again the data which is in the text file is loaded into a new array, (aone) which is then input into the correpsonding text boxes. This all work fine.

                      Except there are commas in the array which i would like to disappear when the text is input into the text box when the application is loaded.

                      the 'undefined' comes from if i dont fill in the exam paper in the correct order. Say i miss question B (parts 1, 2 & 3) out completely..........there will be 3xundefined, in question A's text box.......this is because in the text file the variables are all seperated by '&QuestionOneA=' etc. and this is what the second array looks for. However if a whole question is missed out 'undefined' goes into the array, and because there is no '&QuestionOneB=' for the array to pick out, the array presumes that the 3xundefined are part of the answer to A and so appear in the text box.

                      That probably doesnt make any sense as it is hard to explain without looking at the application, but i know what is going wrong and why i just dont know how to fix it argh!!!!

                      cheers
                      • 9. Please help - printing arrays problem
                        Peter Lorent Level 2
                        Well, as long as you know the 'array' is coming in as a string and you should first use the methods of the String Class (String.split(",") for instance) to build an array you can work with.
                        • 10. Re: Please help - printing arrays problem
                          Jude86_7 Level 1
                          yep its definetly at string........sorry to sound really stupid here.........so i need to put String.split(",") where do i put this?
                          • 11. Re: Please help - printing arrays problem
                            Peter Lorent Level 2
                            See attached code for an example.

                            • 12. Re: Please help - printing arrays problem
                              Jude86_7 Level 1
                              ok cool

                              where u have the buildArray("Text2,,Text2,Text3");

                              what does this do/mean??
                              • 13. Re: Please help - printing arrays problem
                                Peter Lorent Level 2
                                The function buildArray() accepts a string as a parameter (the text with the commas you are receiving) and creates an array from the string using the comma as a delimiter and eliminating the blanks.

                                buildArray("Text1,,Text2,Text3,"); calls the function and you should call it using your own text:
                                buildArray("your text with the commas");

                                From that point you can use the myArray as input for other functions that accept an array as a parameter.
                                • 14. Re: Please help - printing arrays problem
                                  Jude86_7 Level 1
                                  but i dont know what my text will be to put into the build array..........it just depends on what the user enters into the application???
                                  • 15. Re: Please help - printing arrays problem
                                    Jude86_7 Level 1
                                    Does this mean that i cant actually remove the commas if i dont know what the text is going to be??

                                    This is why the first example didnt work too

                                    Please please help im tearing my hair out here

                                    argh!!!!!!!
                                    • 16. Re: Please help - printing arrays problem
                                      dad_1953
                                      I believe that you are making this way to hard on your self. If I understand you correctly you want to do the following.
                                      1. Ask several question and save the answers to a file
                                      2. At sometime in the future you want to load the answer file in to an array in your flash document

                                      Assuming that this is correct then you would need to do the following:
                                      For the Answer file:
                                      You need make this a delimited file. For example a Comma delimited file. The delimiter will need to be a character that will not be in the answer. If these are multiple guess questions then you can use a comma for the delimiter. If the answers are unlimited text answers then you may want to think about using something with an ascii value greater then 125. After the student has finished the test then you will save your answer file using the delimiter of your choice.

                                      Reading in the answer file.
                                      You will read the file and parse it into the answer array based on the delimiter uses.


                                      The above information is at a high design level. I will leave it up to you to figure out how to code it. I would suggest that you pseudocode it first. And then if you have any question on the coding format then you ask them.

                                      Steve
                                      • 17. Re: Please help - printing arrays problem
                                        Peter Lorent Level 2
                                        >>i am currently loading data from a text file into flash using an array and loadVars.
                                        Okay, so the text comes in as a String (and not as an Array which is not possible in Flash).
                                        1. in the LoadVars code your incoming text must be assigned to a variable in Flash.
                                        2. if other functions in your code work with an array, you need to convert the string from 1. to an array and you can use the function I provided to split the string and create an array.
                                        3. the elements in the array ca be assigned to the text fields on your stage.

                                        At least show the code that gets the text - the code with the LoadVars object. Or show the complete code. We could then help you.
                                        • 18. Re: Please help - printing arrays problem
                                          Jude86_7 Level 1
                                          this is the code i have atm..........i got help to remove the 'undefined' off another post i had open..........and i finally got it working........will the removing comma's be along the same lines??

                                          _global.aone = new Array();
                                          _global.aone[0] = new Array();
                                          _global.aone[1] = new Array();
                                          _global.bone = new Array();
                                          _global.bone[0] = new Array();
                                          _global.bone[1] = new Array();
                                          _global.bone[2] = new Array();
                                          _global.cone = new Array();
                                          _global.cone[0] = new Array();
                                          _global.cone[1] = new Array();
                                          _global.done = new Array();
                                          _global.done[0] = new Array();
                                          _global.done[1] = new Array();
                                          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);
                                          _global.aone[1] = removeUndefined(mydata.QuestionTwoA);
                                          _global.bone[0] = removeUndefined(mydata.QuestionOneB);
                                          _global.bone[1] = removeUndefined(mydata.QuestionTwoB);
                                          _global.bone[2] = removeUndefined(mydata.QuestionThreeB);
                                          _global.cone[0] = removeUndefined(mydata.QuestionOneC);
                                          _global.cone[1] = removeUndefined(mydata.QuestionTwoC);
                                          _global.done[0] = removeUndefined(mydata.QuestionOneD);
                                          _global.done[1] = removeUndefined(mydata.QuestionTwoD);
                                          } else {
                                          trace("Failed to load text.");
                                          }
                                          trace(_global.aone[0]);
                                          trace(_global.aone[1]);
                                          trace(_global.bone[0]);
                                          trace(_global.bone[1]);
                                          trace(_global.bone[2]);
                                          trace(_global.cone[0]);
                                          trace(_global.cone[1]);
                                          trace(_global.done[0]);
                                          trace(_global.done[1]);
                                          };