NAVIGATION:

 

SUPPORTERS:

FlashComponents
  Flash Templates
  Flash Gallery
  Flash Slideshow
  Flash Menu
  Flash Design
  Flash Video
  User Interface

 

FOLLOW:

RSS it down! Twitter it up! Facebook it like a fiend!

 

 

Finding Values in an Array
       by kirupa  |  4 September 2005

In Flash, you have the indexOf function that allows you to find text in an array of string data. That is great if you are only working with strings, but you will often run into situations where your array is more than just strings. If your array contains numbers, object references, etc., your indexOf function will not find a non-string value you search for.

So for the many cases that you need to scan through an array for a particular non-string data, the following function may come in handy:

contains = function (input, arrayData) {
for (i=0; i<arrayData.length; i++) {
if (arrayData[i] == input) {
return 1;
}
}
return -1;
};

How to Use It
Using the above function in your code is not complicated. Simply copy and paste the above function into the Actions window in Flash. With your code in place, you need an array that you will use to search through data for.

The function takes in two arguments, the keyword and the array. If your keyword is among the data found in the array, the function returns a 1. Otherwise, if the data can not be found, the function returns a -1.

Ideally, you would use the above function in conjunction with a conditional statement such as an if statement in the following example where I search for "kirupa" in an array called dataValues:

if(contains("kirupa", dataValues)==1) {
trace("Kirupa is found!");
}

Usage Examples
Copy and paste the following array and test cases into your Actions window after your contains function:

dataValues = ["kirupa", 10, Math.PI, "all have nothing in common!"];
// Test 1
trace(contains("kirupa", dataValues));
// Test 2
trace(contains("10", dataValues));
// Test 3
trace(contains(10, dataValues));
// Test 4
trace(contains(Math.PI, dataValues));
// Test 5
trace(contains("common", dataValues));

Press Ctrl + Enter. Notice the output of your various test cases. All of your tests should return a 1 (value found) with the exception of Test 5 that returns a -1 (value not found).

There are a few things that are interesting about the results. Notice that Tests 2 and 3 both return a 1, but 10 is a number in Test 2 but a string value in Test 3. The reason for that is because our contains function does not do type-checking. Our == operator in the for loop doesn't differentiate between a number and a string of the same number.

The lack of differentiation does not hold true for strings and variables though. Checking for the string "kirupa" and the variable kirupa will not equate. Flash will try to find what the variable kirupa is linked to as opposed to assuming that kirupa is equal to the string "kirupa".

You can read about more implementations of this code in the following thread: http://www.kirupa.com/forum/showthread.php?t=190234.

Why the Code Works
Our contains function takes in two arguments, the keyword and the array, and does two things:

  1. It cycles through values in your array.
  2. It checks to see if the value you are currently on is the same as the keyword you are searching for.

The cycling is accomplished with a for loop that goes from index position 0 up to the total length of your array:

for (i=0; i<arrayData.length; i++) {
if (arrayData[i] == input) {
return 1;
}
}

For each iteration of your for loop, the variable i, increments accordingly. Since i is our index position, I can access the data from our array with the following command: arrayData[i]. If that value equals our keyword, then I return a 1 to indicate that the keyword has been found in our array:

for (i=0; i<arrayData.length; i++) {
if (arrayData[i] == input) {
return 1;
}
}

If after cycling through our array the value has not been found, I return a -1 to simulate a failed search return value similar to Flash's indexOf function.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 

Cheers!

Kirupa Chinnathambi
facebook | twitter

 

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)