6 Replies Latest reply on Jun 22, 2006 4:22 PM by Newsgroup_User

    Searching Arrays

    bsalles Level 1
      Hey there,

      I have arrays being created my movie. Some with one numeric value, some with many. Is there a way to just check if a value is in an array? Not where in the array, just, "Hey, Array1, is there a number 12 in you? No. Hey, Array2, is there a number 12 in you? No. Hey, Array3, is there a number 12 in you? Yes? Return Array3."

      Thanks gang,
      -Benjamin
        • 1. Re: Searching Arrays
          .:}x-=V!P=-x{:. Level 3
          put your arrays into an array and then use a for loop to run though checking them all and return the matching array and node node....simple!
          • 2. Re: Searching Arrays
            Level 7
            Benjamin,

            > Is there a way to just check if a value is in an
            > array? Not where in the array, just, "Hey, Array1,
            > is there a number 12 in you?

            You do this by looping through the array. Take a look at the "Array
            class" entry of the ActionScript 2.0 Language Reference to see what Array
            methods, properties, and events are available to you. All objects in
            ActionScript are defined by classes, and arrays are just one type of object.
            So are movie clips, text fields, sounds, you name it.

            The Array class happens to feature an Array.length property, which
            returns the number of items in the array. This means that any array you
            create in ActionScript has this length property. Assuming your instance
            name is Array1, you could do this:

            var total:Number = Array1.length;
            var counter:Number;
            for (counter = 0; counter < total; counter++) {
            if (Array1[counter] == 12) {
            // do something
            }
            }

            The Language Reference will explain how for() statements work, and what
            the ++ (increment) operator is all about.


            David
            stiller (at) quip (dot) net
            Dev essays: http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: Searching Arrays
              bsalles Level 1
              Thanks David,

              Myabe I should have been clearer. There's some Arrays with one value, some with multiple, and all told there's hundreds. When the user enters a value my code has to find which Array houses that value. A FOR loop that runs the length of the array, for each array, takes a long time and causes severe delay in the app's performance, even manages to crash it from time to time.

              Anything simpler?

              -Benjamin
              • 4. Re: Searching Arrays
                NSurveyor Level 2
                I don't know if this is any better, but you could try something like:

                if(new String(","+myArray.join(',')+',').indexOf(','+QUERY+',')!=-1){
                ...
                }

                However, this can only find strings and numbers, and can't distinguish a number from a string...
                • 5. Re: Searching Arrays
                  bsalles Level 1
                  Thank you guys so much.

                  indexOf()

                  Brilliant. Again, thank you.
                  -Benjamin
                  • 6. Re: Searching Arrays
                    Level 7
                    Benjamin,

                    > Thank you guys so much.
                    >
                    > indexOf()

                    I'm glad that works for you, but I have to say, I'm a bit confused that
                    it does. What are you doing with 100s of arrays in the first place? Why
                    put values in an array if that particular array only has one element?


                    David
                    stiller (at) quip (dot) net
                    Dev essays: http://www.quip.net/blog/
                    "Luck is the residue of good design."