Inform 7 Home Page / Documentation


§16.5. Choosing rows

The following would be one way to print out a list of recent Kings and Queens:

paste.png 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:
    say "[accession in row N of the Table of Recent Monarchs]: [name in row N of the Table of Recent Monarchs] ([family in row N of the Table of Recent Monarchs])."

This works, but is repetitive. We often want to work on a single row for a while, either to change things or think about the contents, and it is tiresome to keep specifying the row over and over again. The following shorthand provides some relief:

choose a/the/-- row (number) in/from (table name)

This phrase selects the row with the given number. Row numbers in a table start from 1, so

choose row 1 from the Table of Recent Monarchs

selects the top row.

That allows us to improve the loop:

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:
        choose row N in the Table of Recent Monarchs;
        say "[accession entry]: [name entry] ([family entry]).";

Actually, as we'll see in the next section, this kind of loop is needed so often that there's a shorthand wording for it.

Note that since "accession" is a column name, "accession entry" means the entry in that column of the currently chosen row. This notation can only be used if a "choose" has certainly already happened, and it is a good idea to make that choice somewhere close by in the source code (and certainly in the same rule or phrase definition) for the sake of avoiding errors. We can also choose rows by specifying something about them, like so:

choose a/the/-- row with (table column) of (value) in/from (table name)

This phrase selects the first row, working down from the top of the given table, in which the given column has the given value. Example:

choose row with a name of "Victoria" in the Table of Recent Monarchs;

A run-time problem message is produced if the value isn't found anywhere in that column.

Sometimes it will happen that a column's name clashes with the name of something else: for instance, if we call a column "apples" but we also have a kind called "apple", so that the word "apples" could mean either some fruit or the column. Inform will generally prefer the former meaning as more likely. In case of such trouble, we can simply refer to "the apples column" rather than just "the apples": for instance, "choose row with an apples column of..." rather than "choose row with an apples of..."

We can also choose a row quite at random:

choose a/the/-- random row in/from (table name)

This phrase makes a uniformly random choice of non-blank rows in the given table. Note that although a table always has at least one row, it can't be guaranteed that it always has a non-blank row, so it's possible for this to fail: if it does, a real-time problem message is thrown.


arrow-up.png Start of Chapter 16: Tables
arrow-left.png Back to §16.4. Changing entries
arrow-right.png Onward to §16.6. Repeating through tables