// parameters:
// needle: element that needs to be found; haystack: array that needs to be searched
// returns:
// postion of the element, if found; false, if not found
// please note that if the element is found on top of array, the index returned
will be 0
// so if you are planning to compare the result against FALSE, you should use
like
// if (array_search(needle,haystack)===false) - note the === rather than ==
// using === we can do the exact comaprison (type of value returned is compared
too)
// so 0 (integer) will not be equal to false(boolean)
function array_search (needle, haystack) {
var i;
var found=false;
for (i=0; i<haystack.length; i++) {
if (haystack[i]==needle) {
found=true;
break;
}
}
if (found==true)
return (i);
else
return (false);
}
// parameters:
// array1 & array2: the arrays for comparison; exact can be true or false
which tells the function to compare for length of arrays too
// so if exact is false, comparison for [1,2,3] & [1,2,3,4] will be true
// if exact is true, comparison for [1,2,3] & [1,2,3,4] will be false
// returns:
// true on match; false on mismatch
function array_compare (array1, array2, exact) {
var mismatch=false;
var len;
if (exact && array1.length!=array2.length) {
return (false);
} else {
len=Math.min(array1.length,array2.length)
for (i=0; i<len; i++) {
if (array1[i]!=array2[i])
{
mismatch=true;
break;
}
}
}
if (mismatch==true) {
return (false);
} else {
return (true);
}
Hi Guys,
i have created a login page for in my flash file but i want to which person is logdin then all the place which need to be replace his name its done automatically i hope you guys understand what i need if any knows about that plz tell me :(
i'll be very thank full to you :)
And then it was 2d.. posted by: Sabbie on:Jun 4, 07 5:47 am
Thanks, updated it for 2d arrays. Returns an array with on the [0] the position of the array that matches and on the [1] the position of the variable inside the array.:
function array_search (needle, haystack) {
var i; var ii; var found=false;
for (i=0; i<haystack.length; i++) {
for (ii=0; ii<haystack[ii].length; ii++) {
if (haystack[i][ii]==needle) { found=true; break; }
}
if (found == true) { break; }
else if (haystack[i]==needle) { found=true; break; }
}
resultSet = [i,ii];
if (found==true) { return (resultSet); }
else return (false);
}