Up: Record Samples   [Index]


13.5.19.1 Traversing Records

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

record r;
text s;

...

if (r_first(r, s)) {
    do {

...

    } while (rsk_greater(r, s, s));
}

Or:

for (s in r) {

...

}

Starting from the last, backwards:

record r;
text s;

...

if (r_last(r, s)) {
    do {

...

    } while (rsk_less(r, s, s));
}

Traversing may be started at any point.

Traversing a record mapping integers for strings and starting at "mid":

record r;
text s;

s = "mid";
if (rsk_upper(r, s, s)) {
    integer a;

    a = r[s];
    do {
	o_form("~: ~\n", s, a);
    } while (rs_g_integer(a, r, s, s));
}

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

r_wcall(r, o_, 0, -2, ": ", "\n");

The function will see ‘o_’ called for each record 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;
record r;
text s;

for (s, a in r) {
    o_(s, ": ", a, "\n");
}

See Record Statements.

See The For Statement.