Successfully Searching Multidimensional Arrays in AS3

Please be aware that this tutorial is intended for intermediate-to-advanced ActionScript programmers and designers. It is therefore assumed that you have a competent understanding of the underlying principles and specifics of both versions of ActionScript. I cannot respond to all questions regarding this code. So, please assume that you are on your own. Notes, instructions, and any relevant documentation has been included in the FLA files. Also, you will need Flash CS3 or CS4 to read the source files included in this tutorial.

The Problem
There are times when you are creating something in Adobe’s Flash content creation suite, that you want to be able to have an array of arrays. These are called multidimensional arrays, and they are great for many purposes. Their only big drawback seems to be the lack of searchability in the entire array and sub-array elements.

Multidimensional arrays are like a large red box, with medium-sized blue boxes inside and even smaller yellow boxes in each medium-sized blue box. So, you have the parent array (the red box) and its individual elements (the blue boxes). Each blue box then contains its own array (the yellow boxes).

I Just Can’t Find Anything!
Normally, when you need to find an occurance of some string in another string, you use the handy “indexOf” method like this:

var myIndex:Number = sourceString.indexOf( "my search target" );

And it will generate either “-1″ (not found) or an integer which is equal to the target string’s starting position in the source string.

Then try using the “indexOf” method on a regular array and you also get the results you expect. But try the same method a multidimensional array and things just fall apart. You get nothing but the dreaded “-1″ result: the string you were looking for was not found anywhere in the array. Even if you search in each array element individually, you must still look for only EXACT matches (===). You cannot find partial matches like you can when searching a standard string.

Let there Be Light.
Ooooooh! Pretty colors… I have finally come up with a SIMPLE, quick solution to finding strings in multidimensional arrays. I do it by using the “forEach” method on an array. It passes the array element, index, and entire array object to a function which can then pick through the contents to look for partial matches and return an array on the array object that contains ALL of its instances in the parent array. If you read the results array, it is a set of row and column indexes that can be used directly on your parent array to display the contents of the elements that contain your search target string. It all sounds a bit confusing, I know it took me a while to get my head around the problem, but once you read and run the code below, it will become really clear just how simple and useful this code can be.

And Now, the Code, Please…Click here to download the CS4/CS3 FLA file. You may use this code as part of any project as long as you do not sell my code on its own or as part of any large, commercial project. This code is copyright 2010 Clarence “exoboy” Bowman, all rights reserved.

Also, Here is the Code For you Copy-Pasters// set up a multidimensional array that contains some data
var myArray:Array = new Array();
myArray.push(["granola","people... are great"," 4 ","10"]);
myArray.push(["bill","orangutan","buster","keaton"]);
myArray.push(["steve","gates","24","yes, sometimes"]);
myArray.push(["help","dave","jobs","hal"]);

// here we set up some properties on the array object to hold our search string and our results
myArray.myTarget = "ste";
myArray.myResults = [];

// now we call the search
myArray.forEach(multiSearch);

// this is the function that does all the heavy lifting....
function multiSearch(element:*, index:int, array:Array)
{
// see if we have a match in this array and pass back its index
for(var i:* in element)
{
if( element[i].indexOf( array.myTarget ) > -1 )
{
var tempArray:Array = array.myResults;
tempArray.push([index,i]);
array.myResults = tempArray;
}
}
}

// -------------------------------------------------------------------------------
// all the code below is OPTIONAL... it is just to show our results
// in the output window in Flash so you know it worked....
var printArray:Array = myArray.myResults;
for(var i:* in printArray)
{
trace("TARGET FOUND @: "+printArray[i][0]+", "+printArray[i][1]+" = "+myArray[ printArray[i][0] ][ printArray[i][1] ]);
}
// -------------------------------------------------------------------------------

Share this:

Like this:

LikeLoading...

This entry was posted on Thursday, July 15th, 2010 at 1:28 am and is filed under Latest. You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.

Post navigation

3 Responses to Successfully Searching Multidimensional Arrays in AS3

Thank you for allowing me to search a multidimensional array – this has been working great up until I needed to search for every string inside of another array… let’s say an array that holds 6 names of objects, and i want to find where each of those 6 are located inside of my multidimensional array. Note, each of the 6 names would always be in a different multidimensional array’s element (so i wouldn’t ever find two of the strings in the same element).

The problem is, I set up my for loop, it runs based on the length of my string storage array (in this case, there are the 6 names, so we loop 6 times).

Each time we loop, i want the multiSearch function to change its target we’re searching for, something like this:

myArray.myTarget = nameArray[i];

and ‘i’ woudl just bump up, targeting the next string in my array to then be what we’re searching for. The problem is, i can only ever “find” the very first element.. so if I trace each time I loop – maybe the first element is located at [15] in the multidimensional array – once we loop through 6 times i get:

15
0
0
0
0
0

where what I need it to trace is:
15
16
19
20
23
24

So we found the location of the first one, but all the others show 0 as their position. Let me know if this makes sense thanks!!

Sorry, but this code is really meant to simply be a starting point. Not a total solution. It sounds like what you need to do is to add an outer loop that loops through the entire array of search targets.

I am afraid because of my current work load that is the best advice I can give right now.

I would suggest going to stackoverflow.com and posting your code and question. There are TONS of people who have a lot of time to answer, in great (sometimes excrutiating) detail on subjects just like this.