14 Replies Latest reply on Aug 16, 2006 7:59 AM by sbryner

    Search an Array

    sbryner Level 1
      Hello,

      I need help search an array for a number value. I"m not sure how far off I am. I've been through the tutorials for about 3 hours now. Here is what I hve come up with
        • 1. Re: Search an Array
          kglad Adobe Community Professional & MVP
          the text property of a textfield is a string. so, you'll want to convert that to a number if that's what your comparison is looking for. and you'll need to populate your array some other way:

          • 2. Re: Search an Array
            sbryner Level 1
            didn't seem to work. it's always pulling up
            "bad number" even it is a match.

            Is there something I could be missing?

            could you comment out the function so I can understand
            the steps you are taking for that? It would really help
            me understand arrays and all this more.

            thanks,
            • 3. Re: Search an Array
              sbryner Level 1
              didn't seem to work. it's always pulling up
              "bad number" even it is a match.

              Is there something I could be missing?

              could you comment out the function so I can understand
              the steps you are taking for that? It would really help
              me understand arrays and all this more.

              thanks,
              • 4. Re: Search an Array
                kglad Adobe Community Professional & MVP
                there's a typo in my code. use:

                • 5. Re: Search an Array
                  sbryner Level 1
                  Hello again, Sorry but this seems to not work.

                  It still places "bad number" in the output box even if
                  it is a valid number (ie... 1,2 or 3) or an invalid number(ie...12,14 or 16).

                  what could be wrong? I've copied and pasted it in and it just doesn't
                  seem to work?

                  What are the possibilities that could be wrong?
                  Thanks for the help.

                  Sky
                  • 6. Re: Search an Array
                    sbryner Level 1
                    Now it works. I don't know what I did to screw up your code. But I recopied it and it works.

                    Thank you,

                    Off to learn more array stuff.

                    If anybody could comment out this code so I can understand it that would be great,

                    function testF(a,n){
                    var noMatch=true;
                    for(var i=0;i<a.length;i++){
                    if(a ==n){
                    noMatch=false;
                    break;
                    }
                    }
                    if(noMatch){
                    trace("bad number");
                    } else {
                    trace("Good Number");
                    }
                    }


                    Thanks,

                    sky

                    • 7. Re: Search an Array
                      Level 7
                      You can simplify the code a little first:

                      function testF(a, n){
                      for(var i = 0; i < a.length; i++){
                      if(a == n){
                      return true;
                      }
                      }
                      return false;
                      }


                      If you need further explanation just say the word.


                      Dave
                      • 8. Re: Search an Array
                        sbryner Level 1
                        Thanks Dave,
                        The thing is I'd like to know what this means?
                        in english

                        //for the variable i equals 0; i is less than myArr.length(the length of characters in the array? ; i++ (means add one to i(i=0 +1))??

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

                        // if myArr is equal to myNum then:
                        if(a == n){

                        Plus, you don't need to put an
                        }else{ in there?

                        you can just do a
                        }
                        }
                        does that mean the same?

                        Is this close?
                        Thanks,

                        sky
                        • 9. Re: Search an Array
                          kglad Adobe Community Professional & MVP
                          this forum interprets [ i ] as encoding italic font (unless you use the attach code option) and that caused dave's code to be messed up. it should be:

                          • 10. Re: Search an Array
                            kglad Adobe Community Professional & MVP
                            commented:

                            • 11. Re: Search an Array
                              sbryner Level 1
                              Thanks, Kglad that helps me out a ton.
                              • 12. Re: Search an Array
                                kglad Adobe Community Professional & MVP
                                you're welcome.
                                • 13. Re: Search an Array
                                  Level 7
                                  >>for the variable i equals 0; i is less than myArr.length(the length of
                                  characters in the array? ; i++ (means add one to i(i=0 +1))??

                                  What this means is to have the for loop execute starting with 0 (var i =
                                  0) and then, as long as i is less than the number of 'items' in the
                                  array, to increment i by 1 (i++). So if there are three items in your
                                  array, i will be 0, 1 and 2 - the indexes of your array items.

                                  There are not 'characters' in an array, there are 'items'. Characters
                                  are the individual elements of a string.

                                  As kglad said you don't need an else because if the return executes the
                                  function itself will terminate. the two brackets close the if statement
                                  and the for loop respectively.

                                  If the for loop finishes, and no match was found, the return false will
                                  execute - letting you know that no match was found.

                                  HTH

                                  Dave
                                  • 14. Re: Search an Array
                                    sbryner Level 1
                                    Thanks Dave,

                                    That's explaining it a lot more.

                                    sky