Function match
has the following syntax:
match( a, value )
or
match( a, value, index )
where a
is an array and value
is the value to search for. The optional integer index
is the index of the array entry where the search begins. If no index is specified, the search begins at the first entry of the array.
Function match
returns -1
if no match is found.
Example,
To count all occurrences of a given object inside an array, use the matchcount
function.
matchcount(a, value)
returns the number of occurrences of object value
in array a
. The count start at the beginning of the array.
matchcount(a, value, index)
returns the number of ocurrences of object value
in array a
. The count start at index index
of the array.
Example. Given,
The number of occurrences of number 3 is:
Function find
has this syntax:
find( a, f )
or
find( a, f, index )
where a
is an array, f
is a function of one parameter that returns a boolean value. The optional integer index
is the index of the array entry where the find begins. If no index is specified, the find begins at the first entry of the array.
Function find
returns the index i
of the first entry a[i]
such that f(a[i])
returns a true
value. If no such an entry is found, find
will return -1
.
For example, let's assume you need to locate all values x
such that 3<=x
and x<=5
. A suitable function is:
Assuming array a
is given by:
Then, the first entry found is:
To find all entries that hold a condition you may use function findAll
described below, or you may create a function as follows:
#----------------------------------- # Finds and prints all entries found #----------------------------------- myfind( a, f ) = { print( "Entry/value\n" ); i=1; while( (j=find(a,f,i)) != -1 ) { i=j+1; print( j, " ",a[j], "\n"); } }
Using myfind
with the previous array, we obtain,
To obtain an array with all entries that satisfy a given condition, use function findall
.
findall(a, f)
returns an array with all entries a[i]
of array a
such that f(a[i])
is true.
findall(a, f, index)
returns an array with all entries a[i]
of array a
such that f(a[i])
is true. The find starts at entry index
.
Example. Using array a
and condition f
from the previous example, we obtain:
The contents of arrays can be sorted using the sort()
function. By default,
the sort()
function sorts the contents of an array in ascending order. Function
sort()
uses operators <
and >
to sort the entries
of the array.
Example:
Example:
If the contents of an array are neither real numbers nor strings, then you may use the
sort()
function with a custom comparator. Your custom comparator must be a
function of two arguments, e.g. f(a,b)
, and it must return -1 if object a
is smaller than object b
. It must return 1 if object a
is
bigger than object b
. Otherwise it must return 0.
Next example shows a custom sorting of complex numbers. The idea is to sort all complex numbers with no imaginary part first; then sort the numbers with imaginary parts just by comparing the absolute value of the imaginary part.
Notice how we use library function comp()
in this example.