23 Replies Latest reply on Jul 27, 2007 6:25 AM by kglad

    Finding an index of an array

    Tony_CI
      Hi all,
      Firstly, thanks kglad for your help on getting a CSV imported and parsed, all works well, but now...
      I am having real trouble finding an index of an array based on an input that a user puts in (their unique ID).
      My CSV data contains 3 records, ID, First Name, Surname. What I ultimately want to do is for them to put their ID into a text box, e.g. 56777 and have it look up which array index contains 56777 (e.g. 34) and return me with that number so I can use it to display their firstname and surname. At the moment, if I put in the array index into my text box, i.e. 34, then it has no problem with displaying the firstname, surname and id relevant to that index - but my end user doesn't know that they are array index 34, they know they are ID number 56777!
      I hope someone can explain what I'm doing wrong, because I know the code works if I define an array manually (e.g. TempArray = ["blue", "red", "blue", "green", "yellow", "blue"]) - it tells me that blue appears in 0,2 and 5 - I just can't get it to look up my parsed data.
      Big thankyou to anyone who can shed some light on this frustrating problem!!
      Anyway, I hope that has explained the problem, here is the code I have at the moment for the whole frame....
      .
        • 1. Re: Finding an index of an array
          kglad Adobe Community Professional & MVP
          your last lines of code shouldn't execute until after findPos has executed.

          p.s. you shouldn't be using an onEnterFrame loop to assign myText's text property.

          p.p.s this is a good place to use an associative array instead of an regular array:

          assocA=new Object();
          assocA[ID] = [firstname, surname];
          • 2. Re: Finding an index of an array
            Tony_CI Level 1
            Thanks again kglad for responding.
            How would you suggest to stop it from executing before I run the ar variable? I've even tried putting the second part onto the next frame but it is still tracing not found (and the variable ar is tracing undefined), so maybe I'm thinking its the ar variable where the problem is because it works fine with a manually defined array list.
            Your thoughts and help would be greatly appreciated... thanks very much!
            • 3. Re: Finding an index of an array
              kglad Adobe Community Professional & MVP
              my mistake. you can access findPos any time after the prototype code executes.

              make sure the "value" you're passing from your data base contains no whitespace. ie, strip trailing spaces, new lines and carriage returns. you can check that this is the problem by using trace(value+" "+value.length) in your prototype function.
              • 4. Re: Finding an index of an array
                kglad Adobe Community Professional & MVP
                p.s. _level0.data.ID is an array, correct?
                • 5. Re: Finding an index of an array
                  Tony_CI Level 1
                  Hi again kglad,
                  Yes that is correct, _level0.data.ID is an array.
                  _level0.data[2].ID would be 4423 _level0.data[2].First would be Bob _level0.data[2].Surname would be Smith
                  _level0.data[6].ID would be 4199 _level0.data[6].First would be Jim _level0.data[2].Surname would be Jones

                  I'm pretty sure there are no 'white spaces' in the database but given it was generated out of an external program not excel or such it may be. I will have to check that tonight, thanks for that trace code to check, that is a good trick!
                  Thanks again,
                  Tony
                  • 6. Re: Finding an index of an array
                    kglad Adobe Community Professional & MVP
                    you're welcome.
                    • 7. Re: Finding an index of an array
                      Tony_CI Level 1
                      I tried putting the trace value + value.length code in the prototype function, but received nothing to the output box.
                      Then I decided to test it by asking it to trace a defined value after the prototype function had finished (e.g. Data[21].Name_First), and that came back as undefined. Which I found very odd, considering that at the start of the code i had asked it to display Data[membernumber].Name_First - where membernum would have been an input such as 21 or 22 etc - into a dynamic text box and it worked fine.
                      So...... why is it able to display Data[21].Name_First into a dynamic text box but not recognise it later on down the code after Array.prototype.findPos has executed???

                      My next trick was to start a new blank document with one frame, and put all the actionscript into the one frame and remove any complications of dynamic text etc. I placed some strategic traces in there which may be helpful in working what is going on.

                      The output was...
                      10's trace is...undefined undefined
                      not found
                      33's trace is...Paul Prasse

                      Where was 12's trace.. in a black hole?
                      This is a 'favour freebie' job I'm doing for a friend and it's driving me insane. I have no problems posting an FLA of the file either. Anything to get this working. Thanks kglad for your help so far I really appreciate it.
                      Anyway, here's the code that I put together as a tester in the one frame
                      • 8. Re: Finding an index of an array
                        kglad Adobe Community Professional & MVP
                        "33's trace is..." should appear first. if it does not your other trace statements are executing before your data has completed loading.
                        • 9. Re: Finding an index of an array
                          Tony_CI Level 1
                          Once again huge thanks for your input/advice. Yes, it's looking like that is the problem indeed, I am thinking that the onData (e.g. TonyData.onData) method would be the best, but I can't get it to work properly. I put the csv load on the first frame with an ondata statement directing it to frame 2, and it actually delayed it a bit before going to frame 2. It executed a trace before introducing the var ar statement, but then when it went to do the findPos, returned a not found value.
                          It's so close, I know it, I can see the finish line ahead.
                          Any thoughts?? Is onData not the way to go??
                          • 10. Re: Finding an index of an array
                            kglad Adobe Community Professional & MVP
                            you need an onData or onLoad method, but that's not enough.

                            your prototype code can execute anytime prior to its first use.

                            your other code should be in a function that's called from your onData method at the same spot you have your trace("33...") statement.
                            • 11. Re: Finding an index of an array
                              Tony_CI Level 1
                              Hello once again kglad,
                              I think I've made progress on this one. I've amended the code so the Array Prototype is the first thing as it doesn't matter when it executes.
                              As you'll see from the code below, the finder variable (which I have renamed from previous incarnations) works with an absolute reference (e.g. finder = TonyData[10].ID;) and traces finder OK, but when findPos introduces then it doesn't work (e.g. finder = TonyData[10].ID.findPos(42703);) it traces finder as undefined.
                              So, I'm assuming findPos is the problem. Can you suggest any fixes for this? Thanks, of course, again.
                              Cheers, Tony
                              • 12. Re: Finding an index of an array
                                kglad Adobe Community Professional & MVP
                                that's a strange name, but what's the following reveal when place just before finder?



                                • 13. Re: Finding an index of an array
                                  Tony_CI Level 1
                                  That traces 47050, which is the correct ID record for index 10.
                                  That's what so frustrating!! as soon as findpos is introduced it's undefined
                                  • 14. Re: Finding an index of an array
                                    kglad Adobe Community Professional & MVP
                                    that's not right. TonyData[10].ID should be an array if you're applying findPos to it.
                                    • 15. Re: Finding an index of an array
                                      Tony_CI Level 1
                                      Here's something interesting. At the time I tested with the trace(value+" "+value.length) code it was returning undefined, but now I have scripted for nothing to happen until the data loaded via onData, so I tried it again.
                                      Just before the finder executes, where you got me to put your trace, i added it in as follows..
                                      trace(TonyData[10].ID);
                                      trace(TonyData+" "+TonyData.length);

                                      and it returned this...

                                      • 16. Re: Finding an index of an array
                                        kglad Adobe Community Professional & MVP
                                        so, TonyData is an array with length 849 that contains objects. now what type is TonyData[10].ID?
                                        • 17. Re: Finding an index of an array
                                          Tony_CI Level 1
                                          I hope I haven't confused you by using that, it is just a test thing to see whether an exact reference to an idex works with findpos. TonyData is my array, [10] is the index of the array, ID is the field from the CSV (I have ID, Name_First and Name in there)

                                          What of course I want to work is...
                                          finder = TonyData.ID.findPos(Membernumber);
                                          But it doesn't work! Membernumber is a number the user enters into a box (their unique ID), so that I can find the array index this unique ID appears in so I can use that to display their name in another box. Because a user knows their unique id, but not what array index they are!!
                                          • 18. Re: Finding an index of an array
                                            kglad Adobe Community Professional & MVP
                                            i don't think i'm confused.

                                            findPos in an array method. it operates on arrays. if TonyData[10].ID is not an array, applying findPos to it will not work.
                                            • 19. Re: Finding an index of an array
                                              Level 7
                                              i don't think i'm confused.

                                              findPos in an array method. it operates on arrays. if TonyData[10].ID is not an array, applying findPos to it will not work.
                                              • 20. Re: Finding an index of an array
                                                Tony_CI Level 1
                                                I think the problem is that I'm confused!
                                                If it helps I can host/send the fla file.
                                                Now that I know the data has loaded, its clear that findpos is not working with this, so which method would be the best for getting the index of the input text from the user in the ID field of TonyData?
                                                I found findpos on a website and thought that's what I need that should work, obviously not...
                                                Of course, thanks again.
                                                • 21. Re: Finding an index of an array
                                                  kglad Adobe Community Professional & MVP
                                                  if TonyData is an array of objects each of which has an ID property and you want to find the index of TonyData that contains the object whose ID property is idValue, use:

                                                  • 22. Re: Finding an index of an array
                                                    Tony_CI Level 1
                                                    That is perfect now!! Thanks so much for all your help!
                                                    • 23. Re: Finding an index of an array
                                                      kglad Adobe Community Professional & MVP
                                                      you're welcome.