使用另一个数组作为搜索条件来搜索多维数组 (Flash As3)

发布于 2024-12-06 09:05:00 字数 823 浏览 0 评论 0原文

长话短说:我想在 AS3 中的多维数组中搜索(在本例中)6 个字符串的位置 - 所有这些字符串都存储在另一个无关的数组中。

长话短说:

一旦我获得每个字符串的位置(在多维数组中),我就知道它的位置,并且可以访问该对象的其他属性 - 所以如果我发现字符串“box3”位于元素 [5 ] 我的多维数组,我现在可以定位:

multiArray[5][3] 返回存储的第四个项目(请记住我们从 0 开始,所以 3 是第四个位置)。

我可以让它工作一次,但我试图根据基本字符串存储数组的长度设置一个 for 循环 - 该数组保存(在本例中)6 个实例名称字符串 - 每次我的 for 循环循环时,我需要在多维数组中搜索下一个连续实例名称。

然后,一旦我找到了所有这些(并将结果存储在一个新的临时数组中),我就可以在每个位置中挖掘我需要的信息。

我唯一能找到的是这里的帖子:

http://exoboy.wordpress.com/2010/07/15/successively-searching-multiDimension-arrays-in-as3/

效果很好,如果我只在数组中搜索一个元素 - 但当我需要使用其代码在 for 循环中查找多个元素时,它就会崩溃。您也可以在该页面上查看我的问题/他们的回复,以获取更多信息。

我最终需要一个简单的运行函数,可以反复重复使用,因为我将在该数组中进行大量搜索以查找我正在构建的模拟。谢谢大家, -埃里克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑依然 2024-12-13 09:05:01

我想你需要两个 for 循环。

使用一个循环在数组中进行搜索(这是您已有的循环代码),并将其包装在另一个执行该搜索循环的 for 循环中。

有道理吗?

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?

笔芯 2024-12-13 09:05:01

此版本使用方法本身 searchArray() 进行循环,并跟踪树中的位置。一旦找到匹配项,它就会将位置输出到 searchResults。然后,您可以对每个 uint 数组使用 getNestedItem() 来检索值。

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;      
        }

    }
}

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;      
        }

    }
}
轮廓§ 2024-12-13 09:05:00

这是您可以使用的基本函数。

显然,您需要循环调用此函数的所有数组进行测试。

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)  )
    }
  }
}

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)  )
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文