DYNAMIC ARRAY.search

Scans the array to find an element that matches the search parameter.

Syntax

search( key STRING, value value-type )
   RETURNS INTEGER
  1. key is the name of the RECORD member, when the array is structured.
  2. value is the value to look for.
  3. value-type is the type of the searched value. It can be a primitive-type like STRING, or a reference to an object such as a base.Channel.

Usage

The search() method will scan the whole array, to find a specific element that matches the value passed as second parameter.

The method returns the index of the first occurrence found.

The method returns zero, if no matching element is found.

Elements set to NULL cannot be found: If the search value passed as parameter is NULL, the method returns zero.

The search() method is equivalent to the following function (same as arr.search("member", val) ):
FUNCTION search(arr, val)
    DEFINE arr DYNAMIC ARRAY OF ... ,
           val STRING
    DEFINE x INT
    FOR x=1 TO arr.getLength()
        IF arr[x].member = value THEN RETURN x END IF
    END FOR
    RETURN 0
END FUNCTION

If the array is structured (DYNAMIC ARRAY OF RECORD), the method returns the index of the first occurrence of an element where the record member specified by key is equal to the value parameter.

If the array is a flat array, such as DYNAMIC ARRAY OF INTEGER, the method returns the index of the first occurrence of the element that matches the value parameter. The key parameter is ignored.

Example

MAIN
    DEFINE a DYNAMIC ARRAY OF RECORD
                 name STRING
             END RECORD

    LET a[1].name = "Mike"
    LET a[2].name = "Phil"
    LET a[3].name = "John"

    DISPLAY a.search("name", "Marc")  -- Shows 0
    DISPLAY a.search("name", "John")  -- Shows 3

END MAIN