Skip to main content
bsalles
Participant
June 22, 2006
Answered

Searching Arrays

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
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer
June 22, 2006
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...

6 replies

Inspiring
June 22, 2006
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."


Translate
bsalles
bsallesAuthor
Participant
June 22, 2006
Thank you guys so much.

indexOf()

Brilliant. Again, thank you.
-Benjamin
Translate
Inspiring
June 22, 2006
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."


Translate
bsalles
bsallesAuthor
Participant
June 22, 2006
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
Translate
Correct answer
June 22, 2006
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...
Translate
Participating Frequently
June 22, 2006
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!
Translate