CalcIt Commands

BINSEARCH(Arr, ValueToSearch, NumericSearch=false, SearchMode=bsHIGH, StartPos=1, Width)

Performs a binary search, Numeric of alphanumeric, in a properly sorted array to find a value. If the value is found then its position is returned according SearchMode:

  • Arr. Array to be searched
  • ValueToSearch. Value to search in Arr.
  • NumericSearch. If TRUE then the search considers ValueToSearch and array contents as numeric. If false then considers them as alphanumeric values. The elements contained in Arr must be sorted numerically or alphabetically (not case sensitive), according to this parameter, for this function to operate correctly. See commands SORT and SORTN. Optional. If omitted then false (alphanumeric search).
  • SearchMode. Affects the way the result is returned. Optional.
    1. bsLOW. If the value found has duplicates then the position of the lower occurrence is returned.
    2. bsHIGH. If the value found has duplicates then the position of the higher occurrence is returned (DEFAULT).
    3. bsBOTH. In the form of delimited text it is returned the position of the lower and higher occurrences of the found value. If no duplicates then low=high.
  • StartPos. The starting position of the part of every element at Arr to search. Optional. The start of every element if omitted. This parameter is not used in case of numeric search.
  • Width. Starting at StartPos a part of Width characters is searched for every element of Arr. Optional. The rest of every element if omitted. This parameter is not used in case of numeric search.

If the searched value is not found then a negative number is returned indicating  the position of the first greater value in the array. If we wish we can insert the value in this position.

See also its generic version below.

BINSEARCH(GetValue, NumOfElements, ValueToSearch, NumericSearch=false, SearchMode=bsHIGH, StartPos=1, Width)

With this generic version of BinSearch we can search on anything, even in a file handled by a File variable. The user can write a local function to return the values the algorithm needs from the searched structure. In the second parameter (NumOfElements) it is defined the number of elements the search operation has to deal. It is assumed that the fist element is at position 1.

function GetValue(p);
 
//return the value at position p
end;

RetValue:=BinSearch(GetValue,100,'Some value to search for');

In the above example the optional parameter for NumericSearch is omitted. Then FALSE is assumed which means alphanumeric search

NOTE: The structure must be sorted properly according to the returned values by GetValue.

For the rest of parameters see its array version above.

Go Back