DYNAMIC ARRAY.search
Scans the array to find an element that matches the search parameter.
Syntax
search( key STRING, value STRING )
RETURNS INTEGER
- key is the name of the
RECORD
member when the array is structured. - value is the value to look for.
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.
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 (DYNAMIC ARRAY OF base-type
),
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