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.

PARTIAL SEARCH FORMATS THAT FAIL

  • myArray.indexOf( “find this” ) = FAILS!
  • myArray[ element0 ].indexOf( “find this” ) = FAILS!
  • myArray[ element0 ][ element0 ].indexOf( “find this” ) = FAILS! (returns the starting locating in the array element string, but not the array index!)

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] ]);
}
// -------------------------------------------------------------------------------

About these ads

3 Responses to Successfully Searching Multidimensional Arrays in AS3

  1. Eric Bort says:

    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!!

    • exoboy says:

      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.

      Thanks for reading my blog and for your comments!

  2. Eric Bort says:

    Thanks, i’ve posted something on stack – hopefully that sends me in the right direction, thanks again for the quick response!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: