5.06 Array Searching

Arrays can be searched using the search command.

Search Command

Function:

If a single value is specified, the search command scans an array for all elements that have a value equal to that value. If a second value is specified all elements with a value between, and including, the two values are selected. All entries that are found are passed to the USE procedure for processing.

Syntax:

search array, value1[, value2] use proc
search array, like wildcard use proc

Processing:

The procedure is passed two variables. The first is the index number of the entry and the second is the entry itself. It does not return a value.

Example:

proc display(index, value)
  put "  index =" index:", value =" value
  end proc display(index, value)

...
tange a(5)
for i = 1 to 5
  a(i) = 5*i
  next

search a, 10, 20 use display

// displays
  index = 2, value = 10
  index = 3, value = 15
  index = 4, value = 20

Like wildcard

Like is a regular-expression operator. It compares each element of the array with the pattern. It calls the procedure for each element where there is a pattern match. See 4.09 IN and LIKE for how to construct the pattern.

proc display(index, value)
  put "  index =" index:", value =" value
  end proc display(index, value)

array1 = {10,"20",five0}
search array1, like '*0' use display
search array1, like '10' use display
search array1, like '20' use display
search array1, like '30' use display
search array1, like '[Ff]*' use display

// displays
  index = 1, value = 10
  index = 2, value = 20
  index = 3, value = FIVE0
  index = 1, value = 10
  index = 2, value = 20
// note no match for 30
  index = 3, value = FIVE0

Search Function

Function:

The search function scans an array for the first entry equal to a specified value, or between two values if a second value is specified. It returns the index of the first match found. If no match is found it returns the value (origin(array)-1). In arrays with a default base value that is zero.

Syntax:

search(array, value1[, value2])

Example:

range a(5)
for i = 1 to 5
  a(i) = 5*i
  next

index = search(a, 10)
put "  index =" index:", value =" a(index)

// displays
  index = 2, value = 10

See Also:


lynplex/lp0506.txt · Last modified: 2013/06/22 15:41 (external edit)