Siebel eScript Language Reference > Siebel eScript Commands > The Clib Object >

Clib.bsearch() Method


This method looks for an array variable that matches a specified item.

Syntax

Clib.bsearch(key, arrayToSort, [elementCount,] compareFunction)

Parameter
Description
key
The value to search for
arrayToSort
The name of the array to sort
elementCount
The number of array elements to search; if omitted, the entire array is searched
compareFunction
A user-defined function that can affect the sort order

Returns

An array variable that matches key, returning the variable if found, null if not.

Usage

Clib.bsearch() searches only through array elements with a positive index; array elements with negative indices are ignored.

The compareFunction value 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 the function searches the entire array.

Example

The following example demonstrates the use of Clib.qsort() and Clib.bsearch() to locate a name and related item in a list:

(general) (ListCompareFunction)

function ListCompareFunction(Item1, Item2)
{
   return Clib.strcmpi(Item1[0], Item2[0]);
}

(general) (DoListSearch)

function DoListSearch()

   // create array of names and favorite food
   var list =
   {
      {"Brent", "salad"},
      {"Laura", "cheese" },
      { "Alby", "sugar" },
      { "Jonathan","pad thai" },
      { "Zaza", "grapefruit" },
      { "Jordan", "pizza" }
   };

   // sort the list
   Clib.qsort(list, ListCompareFunction);
   var Key = "brent";
   // search for the name Brent in the list
   var Found = Clib.bsearch(Key, list, ListCompareFunction);
   // display name, or not found
   if ( Found != null )
      TheApplication().RaiseErrorText(Clib.rsprintf
      ("%s's favorite food is %s\n", Found[0][0],Found[0][1]));
   else
      TheApplication().RaiseErrorText("Could not find name in list.");
}

See Also

Clib.qsort() Method


 Siebel eScript Language Reference 
 Published: 18 April 2003