开发者

AS3 - Searching An Array for the items that contains a particular word when each item is a paragraph long

bit new to AS3 so forgive me if this is a bit of a straight forward question.

Ive created an array, and each item in the array is a description of something, about 20 words long, I want to search the array, and return each item that contains a particular word.

All the other tutorials Ive found 开发者_高级运维are based on looping through to search for an exact match.

Thanks!


No matter what, you'll need to iterate through the array to find the match.

This said, use the String.search() method to look for your word:

var searchTerm:String = "whatever word you're looking for";
var foundItems:Array = new Array();

for each(var paragraph:String in paragraphArray) {
    if(paragraph.search(searchTerm) != -1) {
        foundItems.push(paragraph);
    }
}
return foundItems;

EDIT: I tested with the following code, and it seemed to work ok.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="filterArray()">
    <mx:Script>
        <![CDATA[
            private var arr:Array = ["a", "a b c", "c d e", "a d e", "z q aaa r"];
            private var searchF:String = "a";

            private function filterArray():void {
                var searchTerm:String = searchF;

                for each(var paragraph:String in arr) {
                    if(paragraph.search(searchTerm) != -1) {
                        trace('found: ' + paragraph);
                    }
                }
            }
        ]]>
    </mx:Script>
</mx:Application>

Edit #2: I modified the previous snippet so it looked more like what you had in your comment. I broke out "searchF" and pointed to it from searchTerm in filterArray().


The only efficient way to do what you want is a lookup table (these can get complicated, before you embark on this, do some performance tests - the brute force search approaches listed here may work fine for you).

The basic idea is that you keep a list of all the paragraphs where a particular word occurred. So, if "foobar" appeared in paragraphs 1, 2, and 7, we would want the following to be true:

myLookupTable["foobar"] = [1,2,7];

If we do this for every word in every paragraph, we get a nice little list.

This is the basic idea:

var myHugeArray:Array = // fill your array data in here

var lookup:Object = {};

// Now we iterate through each item in your array and index all the words that we find
for (var idx:int in myHugeArray)
{
    var par:String = myHugeArray[idx] as String;
    par = par.replace(/['",.;:-()!?]/g, " "); // remove common punctuation

    var words:Array = par.split(" ");  // split the paragraph into specific words
    for each(var word:String in words) 
    {
        if(lookup[word] == undefined}
            lookup[word] = [];
        lookup[word].push(idx); // record the index of the paragraph that contained the word
    }
}

// now do search for a word
var results:Array = lookup["foobar"];
if(results != null)
{
    for each(var idx:int in results)
        trace("Hit!", myHugeArray[idx]);
}

Now, you have to update your lookup table whenever you add or remove items from the array (not shown here). In addition, you may want to create a "stop list" of common words such as "the" and "a" that aren't indexed. Finally, this will only return exact matches on words. If you want to be able to type in "foob" and still get hits for "foobar", then you need to create within-word indexes, which are much more complex and memory-intensive.


filter

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#filter%28%29

var myArray = ['aaa', 'bbb', 'cac'];
var needle = 'a';
trace( myArray.filter(array_search) );


function array_search(element:*, index:int, arr:Array):Boolean {
    return (element.indexOf(needle) > -1 ? true : false);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜