Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.4j

Application v 5.22b

  

 

Chapter EightData Functions (continued)

BinarySearchTable Function

Overview

The BinarySearchTable function performs a binary search of a sorted table within a specified column.

Syntax/Parameters

Syntax

int = BinarySearchTable ( string table[][], int column | string column,
                      string target, [int mode] );

Parameters

table

A table of string data which must resolve to two dimensions.

column

An int or string value specifying either the zero-based column index to search or a key name representing the column.

target

A string to search for within the table.

mode

An optional int specifying a match mode as defined below. The default mode is SORT_ALPHA.

Return Value

Returns an int specifying the matching row index position or -1 if the item is not found. When not found, the last error will contain the next index position after where the item would be in the list. Use the GetLastError function to retrieve error code and index information. Note that the index can be passed the last position if the pattern is larger (or smaller in descending mode) than the last position

Remarks

A binary search employs an algorithm that finds the position of a target value within a sorted array. A binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.

Binary search is very fast requiring only log2(n) + 1 iterations. For example, searching a list of 128,000 items will only require 18 compares to eliminate the target on the list or less than 18 to actually match the item. However, it requires the list to be sorted in the same mode as the search. The modes are as follows:

  Term   Code   Description  
  Types (select one)          
    SORT_ALPHA   0x00000000   Sorted As Text  
    SORT_ALPHA_NUMERIC   0x00000001   Sorted As Text Expand Numbers — Numeric values are blown out for comparison such that 12 will precede 100.  
    SORT_NUMERIC   0x00000002   Loose Numeric Matching — Each field is converted to a number using the logic of TextToInteger and then compared.  
    SORT_DATE   0x00000003   Date Mode — Each field is converted to a date value using the loose conversion and then compared.  
  Options        
    SORT_ASCENDING   0x00000000   Sorted in Ascending Order (default)  
    SORT_DESCENDING   0x00001000   Sorted in Descending Order  
    SORT_NO_CASE   0x00004000   Not Case-Sensitive (does not apply to SORT_NUMERIC or SORT_DATE)  

 

The last error will also contain the row index with ERROR_SOFT ORed if the item was not found. When not found, the index will be the next larger item. For example, if the list contains “a”, “b”, “c”, “d”, “f”, “g”, and “h” and the target is “e”, the return value will be -1 and the last error will be 0x80000004, pointing to “f”, the next larger item in the list. If there is no larger item, the error index will be the same as the depth, or 1 plus the last item index. This allows for an insert point or the ability to iterate items from a certain point. For example, all items after the letter ‘N’.

If items are inserted in to the table in the correct order, the table will not require resorting for subsequent searches.

Related Functions

Platform Support

Go13, Go16, GoFiler Complete, GoFiler Corporate, GoFiler, GoFiler Lite, GoXBRL

Legato IDE, Legato Basic