Up: Index Samples   [Index]


13.4.18.1 Traversing Indexes

Indexes may be traversed by keys. Starting from the first to the last:

integer j;
index i;

...

if (i_first(i, j)) {
    do {

...

    } while (isk_greater(i, j, j));
}

Or:

for (j in i) {

...

}

Starting from the last, backwards:

integer j;
index i;

...

if (i_last(i, j)) {
    do {

...

    } while (isk_less(i, j, j));
}

Traversing may be started at any point.

Traversing an index mapping integers for integers and starting at 1001:

integer j;
index i;

j = 1001;
if (isk_upper(i, j, j)) {
    integer a;

    a = i[j];
    do {
	o_form("~: ~\n", j, a);
    } while (is_g_integer(a, i, j, j));
}

Traversing indexes is also possible by traversal functions.

Displaying the keys and associated data for an index i, with a colon and a space in between:

i_wcall(i, o_, 0, -2, ": ", "\n");

The function will see ‘o_’ called for each index element with four arguments: the key (inserted in first position – ‘0’), the colon space string, the associated data in between (before the last position – ‘-2’) and the newline string.

The same, with a ‘for’ statement:

integer a, j;
index i;

for (j, a in i) {
    o_form("~: ~\n", j, a);
}

See Index Statements.

See The For Statement.