View Full Version : Neat Little Array Searcher Function
GrndMasterFlash
November 8th, 2007, 04:04 PM
i just wrote this and thought others out there would like to have a function that finds an objects place in an array.
//BY: Dononvan Hubbard 11-7-07
//arrName is the arrays name, and item is what your looking for in the array
function searchArray(arrName:Array, item:*):* {
//loop through the array
for (var s:int = 0; s < arrName.length; s++) {
//see if the array items matches the item your looking for
if (arrName[s] == item) {
var arrValue = s;
//return your items place in the array
return arrValue;
}
}
}
hope this helps some of you guys :sc:
FizixMan
November 9th, 2007, 01:20 AM
Good show! Keep it up!
Just so you know, you don't need to assign the index value to a temporary variable -- you can just directly return it:
return s;
Also, depending on the size of the array, and the number of iterations, it may be faster to temporarily store the value of the array length rather than resolving it again each iteration:
//loop through the array
var arrayLength:int = arrName.length;
for (var s:int = 0; s < arrayLength; s++) {
It's very minor, and probably not applicable, just some tidbits that you'll eventually stumble upon as you continue programming.
Krilnon
November 9th, 2007, 01:43 AM
I guess that this would be an alternative to using Array.indexOf (http://livedocs.adobe.com/flex/201/langref/Array.html#indexOf())… perhaps for those who do not want to use strict equality for comparisons. It's probably faster to use the built-in method, if speed is a concern.
kirupa
November 9th, 2007, 02:29 AM
That is nice GrndMasterFlash. I've capitalized the words in your title and moved your thread to Source/Experiments, since that may be something that others would be interested in seeing also.
Like Krilnon mentions, though, for most cases, using built-in methods is probably faster.
Cheers!
Kirupa :sailor:
GrndMasterFlash
January 26th, 2010, 04:37 PM
wow, working on an old AS2(blargh!) project and it seems Arrays don't have an index of in AS2, Strings still do but not arrays?
anyways, this old snippet is INDEED useful... to me... and anyone who needs a simple array search. anyways here is the test i ran for AS2, noticed i polished up the function some
var an_arr:Array = new Array("hello", "world", "foo");
trace("using arrayLocation "+arrayLocation(an_arr, "world"));
trace("using indexOf "+an_arr.indexOf("world"));
//
function arrayLocation(arr:Array, item:Object):Number
{
var arr_val:Number = -1;
var s:Number = 0;
while(s<arr.length)
{
if (arr[s] == item)
var arr_val = s;
s++;
}
return arr_val;
}
//
//OUTPUT
//using arrayLocation = 1
//using indexOf = undefined
glosrfc
January 27th, 2010, 12:05 PM
Feel free to play around with this version. It's broadly similar but it does have a couple of slight improvements:
var an_arr:Array = new Array("hello", "world", "foo");
an_arr.sort();
function arrayLocation(arr:Array, item:Object, ins:Boolean){
var l = arr.length, j = -1, k;
while(l - j > 1)
if(arr[k = l + j >> 1] < item) j = k;
else l = k;
return arr[l] != item ? ins ? l : -1 : l;
};
trace("located at index: " + arrayLocation(an_arr, "world"));
// Output:
// located at index: 2
It's a binary search (http://en.wikipedia.org/wiki/Binary_search_algorithm)so it should be significantly faster than your linear search (http://en.wikipedia.org/wiki/Linear_search) particularly on large arrays, and more so where the item being searched for is towards the further end of the array. However, while the speed gains might allay Krinlon's fears, it's only applicable to an ordered array so you do have to sort() it first.
It also has an insert option. When true, this returns the index where the item is found or, if not found, the index where the item should be inserted to keep the array ordered. If the option is false, it returns the index where the item is found or -1 if not found.
trace("located at index: " + arrayLocation(an_arr, "world", true));
trace("insert at index " + arrayLocation(an_arr, "worlds", true));
trace("insert at index " + arrayLocation(an_arr, "words", true));
// Output:
// located at index: 2
// insert at index 3
// insert at index 2
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.