开发者

Search Multidimensional Array (Flash As3) using another array for search criteria

Long story short: I want to search a multidimensional array in AS3 for (in this example) the location of 6 strings - all of which are stored in another unrelared array.

Long story long:

Once I get the locations (in the multidimensional array) of each string, i then kno开发者_如何转开发w where it's located, and can access other atributes of that object - so if i found the string "box3" is located in element [5] of my multidimensional array, i can now target:

multiArray[5][3] to return the 4th item stored (keeping in mind we're starting from 0, so 3 is the 4th position).

I can get this to work once, but I'm trying to set up a for loop based on the length of my basic string storage array - this array holds (in this example) 6 instance name strings - each time my for loop loops, i need to run a search in my multdimensional array for the next consecutive instance name.

Then, once I've located all of them (and store the results in a new temporary array) I can dig around in each location for the info I need.

The only thing I can find is the post here:

http://exoboy.wordpress.com/2010/07/15/successfully-searching-multidimensional-arrays-in-as3/

which works GREAT if I'm only searching for one elements in my array - but soon as I need to find multiple elements in a for loop using their code it falls apart. You can see my question/their response on that page as well for some more info.

I need a simple to run function in the end that can be re-used over and over as I'll be doing a lot of searching in that array for the simulation i'm building. Thanks everyone, -Eric


Here is the base function you could use.

Obviously you will need to loop all your arrays calling this function for the test.

function arrayContains( haystack:Array, needle:String ):Boolean{
  for( var i:Number = 0;i < haystack.length; i++ ) {
    if( needle == haystack[i] ) {
      return true;
    }
  }
  return false;
}


// sample code
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"]);

var searchArray:Array = ["granola","orangutan","24","hal"];

for each( var arr:Array in myArray ){
  for each( var searchString:String in searchArray ){
    if( arrayContains( arr, searchString ) ){
      trace( 'found ' + searchString + 'in myArray at ' + myArray.indexOf(arr)  )
    }
  }
}


I guess you need two for loops then.

use one loop for the searching in the array (that's the loop code you already have), and wrap it in another for loop which executes that searchloop.

Makes sense?


This version loops using the method itself, searchArray(), and keeps track of the position in the tree. Once it finds a match, it outputs the position to searchResults. Then you can use getNestedItem() with each array of uints to retrieve the value.

package  
{
    import flash.utils.getQualifiedClassName;

    public class NestedSearch 
    {
        private var _multidimensionalArray :Array = 
            [
                //[0]
                [
                    // [0]
                    // [0],  [1],    [2]
                    ["one", "two", "three" ], 
                    // [1]
                    // [0],  [1],    [2]
                    ["eleven", "twelve", "thirteen" ] 
                ],
                //[1]
                [
                    // [0]
                    // [0],  [1],    [2]
                    ["hyena", "iguana", "jackal" ], 
                    // [1]
                    "koala"
                ]
            ];

        private var queries :Array = new Array( "three", "twelve", "hyena", "koala" );

        private var searchResults :Array = [];


        public function NestedSearch() 
        {
            // Make multiple queries
            for ( var q in queries)
            {
                searchArray( queries[ q ], _multidimensionalArray, [] );
            }           

            // Use the results
            for ( var i in searchResults )
            {
                trace( 'Found "' + searchResults[ i ].query + 
                      '" at _multidimensionalArray[' + searchResults[ i ].position + ']');  
                trace( "Test value: " + getNestedItem( _multidimensionalArray, searchResults[ i ].position ));
            }
        }

        // Searches any array for an exact String value. 
        // Returns the found string as an Array of uints representing its position in the tree.
        public function searchArray ( s:String, a:Array, positionPath:Array )
        {
            // Duplicate array to save it before going back out
            var _origPosPath = positionPath.concat();   

            for ( var i in a )
            {
                if ( getQualifiedClassName( a[ i ] ) === "Array")
                {
                    positionPath.push(i);
                    searchArray( s, a[ i ], positionPath );
                    positionPath = _origPosPath;
                }
                else
                {
                    if ( a[ i ] === s)
                        searchResults.push( {query: s, position: positionPath.concat( i )} );
                }
            }
        }

        private function getNestedItem ( arr:Array, pos:Array ) :*
        {
            var nestedItem;
            var p = pos.shift();
            if ( pos.length === 0 )
            {
                nestedItem = arr[ p ];
            }
            else 
            {
                nestedItem = getNestedItem( arr[ p ], pos );
            }
            return nestedItem;      
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜