## Sets

### Finding for a match

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,

a = array(10,1,10)*2
2  4  6  8  10  12  14  16  18  20
match(a,11)
-1
match(a,14)
7

### Counting matches

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,

a=(3, 5, 7, 3, 5, 9, 3, 3)
3  5  7  3  5  9  3  3

The number of occurrences of number 3 is:

matchcount(a, 3)
4
matchcount(a, 3, 5)
2

### Finding an element given a condition

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:

f(x) = 3 <= x && x <= 5
f(x) = 3 <= x && x <= 5

Assuming array `a` is given by:

a = ( 1, 3, 7, -5, 4, 2, 5, 7, 4 )
1  3  7  -5  4  2  5  7  4

Then, the first entry found is:

find( a, f )
2

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,

myfind( a, f )
"Entry/value\n"
2  3
5  4
7  5
9  4

### Finding all elements given a condition

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:

findall(a, f)
3  4  5  4
findall(a, f, 5)
4  5  4

### Sorting

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:

a=(5, 1, 7, 11, 9)
(5  1  7  11  9)
sort(a)
(1  5  7  9  11)

Example:

a=("a", "ab", "ca", "cz", "a1")
("a"  "ab"  "ca"  "cz"  "a1")
sort(a)
( "a" "a1" "ab" "ca" "cz" )

### Custom sorting

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.

f(z1, z2) = {
i1=Im(z1);
i2=Im(z2);
// Case both have no imaginary parts
if(i1==0 && i2==0)
return comp(Re(z1),Re(z2));
// Case z1 has no imaginary part
if(i1==0)
return -1;
// Case z2 has no imaginary part
if(i2==0)
return 1;
// Case both have imaginary parts
r1=Re(z1);
r2=Re(z2);
if(r1==r2)
return comp(i1,i2);
return comp(r1,r2);
}
f(z1, z2) = {
i1=Im(z1);
i2=Im(z2);
// Case both have no imaginary parts
if(i1==0 && i2==0)
return comp(Re(z1),Re(z2));
// Case z1 has no imaginary part
if(i1==0)
return -1;
// Case z2 has no imaginary part
if(i2==0)
return 1;
// Case both have imaginary parts
r1=Re(z1);
r2=Re(z2);
if(r1==r2)
return comp(i1,i2);
return comp(r1,r2);
}
x = ( (1.0 + 2.0 j), 3, (1.0 + 7.0 j), (1.0 + 3.0 j), 1 )
( (1.0 + 2.0 j) 3 (1.0 + 7.0 j) (1.0 + 3.0 j) 1 )
sort(x,f)
( 1 3 (1.0 + 2.0 j) (1.0 + 3.0 j) (1.0 + 7.0 j) )