| Chapter 15: Tables |

| 15.6. Repeating through tables

We very often want to run through a table doing something to, or with, each row in turn, so a special loop is provided for this. Rather than having to write all this out:

> To list the succession:
> say "The Succession List runs as follows...";
> repeat with N running from 1 to the number of rows in the Table of Recent Monarchs begin;
> choose row N in the Table of Recent Monarchs;
> say "[accession entry]: [name entry] ([family entry]).";
> end repeat.

We can simply write this instead:

> To list the succession:
> say "The Succession List runs as follows...";
> repeat through the Table of Recent Monarchs begin;
> say "[accession entry]: [name entry] ([family entry]).";
> end repeat.

As we might expect, this runs through the rows in order, starting at the top and choosing each in turn. (No loop variable is needed here.) We can alternatively go through in reverse order, or in order of the values in any column:

> repeat through the Table of Recent Monarchs in reverse order begin; ...
> repeat through the Table of Recent Monarchs in name order begin; ...
> repeat through the Table of Recent Monarchs in reverse accession order begin; ...

Here text is placed in alphabetical order, numbers are in numerical order, times in chronological order and so forth. (In a loop like this, the data is not searched very efficiently, which is fine for modest-sized tables like the examples in this chapter, but might be a problem for much larger tables: see the later section on sorting.)

"Repeat through..." does something else, too: it skips any blank rows in the table, which brings us to the business of blankness.

208
| Example Port Royal 4 A cell window through which the player can see people who were in Port Royal in the current year of game-time.

Previous | Contents | Next