contents   index   previous   next



Clib.bsearch()

syntax:

Clib.bsearch(key, array[, elementCount],

             compareFunction)

where:

key - value for which to search.

 

array - beginning of array to search.

 

elementCount - number of elements to search. Default is the entire array.

 

compareFunction - function used to compare key with each element searched in the array.

 

return:

value - the element in an array if found, else null if not found.

 

description:

This method looks for an array variable that matches the key, returning it if found and null if not. It will only search through positive array members (array members with negative indices will be ignored). The compareFunction must receive the key variable as its first argument and a variable from the array as its second argument. If elementCount is not supplied then it will search the entire array. The elementCount is limited to 64K for 16-bit version of ScriptEase.

 

see:

Clib.qsort()

 

example:

// This example creates a two dimensional array

// that pairs a name with a favorite food.

// A name is searched for. The name and paired

// food is displayed.

 

var Found;

var Key;

var list;

 

   // create array of names and favorite food

var list =

{

   {"Marge",    "salad"},

   {"Lisa",     "tofu"},

   {"Homer",    "sugar"},

   {"Bart",     "anything"},

   {"Itchy",    "cats"},

   {"Scratchy", "anything from the garbage"}

};

   // sort the list

Clib.qsort(list, ListCompareFunction);

 

Key[0] = "marge";

   // search for the name Marge in the list

Found = Clib.bsearch(Key, list, ListCompareFunction);

   // display name, or not found

 

if (Found != null)

   Clib.printf("%s's favorite food is %s\n",

               Found[0], Found[1])

else

   Clib.puts("Could not find name in list.");

 

   // This compare function is used to sort

   // the array and to find a name.

   // The sort and search are case insensitive.

function ListCompareFunction(Item1, Item2)

{

   return Clib.strcmpi(Item1[0], Item2[0]);

}

 


Clib.qsort()