Scott Gibbs Scott Gibbs - 1 year ago 195
ActionScript Question

Actionscript 3.0. Searching an Array for an integar and getting the value's index

I am still fairly new to ActionScript and need a hand with something.

I have an Array filled with randomly generated numbers that are all below 6, "Order", for example, it might contain this.


I need to search it for an integar that is stored in another array. If the integar is present, I would like it to add 1 to a variable "Trial" then get the index of the value in the "Order" so that I can effectively clear that specific index by replacing the value with a value that's outside of the random generation range.

So, for example, if I needed to search for "2" I need my code to replace the first "2" with a 7 for example while leaving the second one alone and adding 1 to the value of the variable "Trial".

I can't think of a way to do this, and my attempts at finding a solution online has only came up with one thread which I didn't really understand because I'm still quite new to Actionscript.

Answer Source

Check this link : Array.indexOf(item);

Which gives you index of item in Array

var Order:Array = [1,2,4,2]; //create the array
Order[Order.indexOf(2)] = 7; //find first position of '2' and replace with '7'

Also indexOf have an optional argument that defines a start position for searching.

indexOf(searchElement:*, fromIndex:int = 0)

Start index could jump within same Array to find target value, for example in following snippet, we have to find index of second '2' in Order array :

trace ( Order[Order.indexOf( 2, Order.indexOf(2)+1 )] ); //outputs '3'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download