Inform 7 Home Page / Documentation
§16.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:
choose row N in the Table of Recent Monarchs;
say "[accession entry]: [name entry] ([family entry])."
We can simply use this instead:
repeat through (table name):
This phrase causes the block of phrases following it to be repeated once for each row in the given table, choosing each row in turn, from top to bottom. Blank rows are skipped. Example:
To list the succession:
say "The Succession List runs as follows...";
repeat through the Table of Recent Monarchs:
say "[accession entry]: [name entry] ([family entry])."
Note that there is no loop variable here, unlike in other forms of "repeat", because it's the choice of row which keeps track of how far we have got.
We can alternatively go backwards:
repeat through (table name) in reverse order:
This phrase causes the block of phrases following it to be repeated once for each row in the given table, choosing each row in turn, from bottom to top. Blank rows are skipped.
More often we want a sequence which is neither forwards nor backwards, but which depends on the actual values in the table.
repeat through (table name) in (table column) order:
This phrase causes the block of phrases following it to be repeated once for each row in the given table, choosing each row in turn, in order of the values in the given column. Blank rows are skipped. Example:
repeat through the Table of Recent Monarchs in name order: ...
repeat through the Table of Recent Monarchs in accession order: ...
work through the same table in rather different orders. The sequence is lower to higher (small numbers to high numbers, A to Z, and so on); insert "reverse" after "in" to reverse this.
repeat through (table name) in reverse (table column) order:
This phrase causes the block of phrases following it to be repeated once for each row in the given table, choosing each row in turn, in order of the values in the given column. Blank rows are skipped. Example:
repeat through the Table of Recent Monarchs in reverse name order: ...
repeat through the Table of Recent Monarchs in reverse accession order: ...
work through the same table in rather different orders. The sequence is higher to lower (high numbers to small numbers, Z to A, and so on); delete the "reverse" after "in" to reverse this.
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.
These definitions mentioned blankness several times, and that's the topic to cover in the next section.
See Sorting for reordering a table to put it into increasing or decreasing order of the entries in any column
|
ExamplePort Royal 4
A cell window through which the player can see people who were in Port Royal in the current year of game-time.
|
|
Our protagonist is imprisoned in Port Royal, waiting out his years, and sometimes through the window of his cell he is able to see someone.
We are, however, obsessive about historical accuracy, so we provide a table of people who really lived in the city, together with the year in which their existence is attested. We want these people to appear in the description only in the year when they are known to have been present. (After all, mortality was high in Port Royal and new people were constantly arriving, so someone's presence one year is no guarantee of their continued existence the next.)
"Waiting for Godot, Chyrurgeon"
New New Prison is a room. "You have a not very interesting room. Through the window you see passing [current denizen]."
Instead of waiting:
increment the current year;
say "It is now the year [the current year].";
try looking.
When play begins: now the right hand status line is "[current year]".
Every turn:
if the current year is 1692:
say "It turns out you have remained imprisoned until the great earthquake of 1692! Oops.";
end the story.
Current year is a number that varies. The current year is 1664.
To say current denizen:
repeat through the Table of Occupations and People:
if the date attested entry is the current year:
say "[nickname entry] [family entry], [trade entry]";
blank out the whole row;
rule succeeds;
say "absolutely no one".
It is possible to look up a row corresponding to, say, a specific year value using "listed in", but repeat through is convenient here because we know that we will never wind up trying to print entries when no row can be successfully selected.
Table of Occupations and People
Trade
|
nickname
|
family
|
Date attested
|
"architect"
|
"Robert"
|
"Snead"
|
1684
|
"baker"
|
"William"
|
"Wingar"
|
1683
|
"barber"
|
"William"
|
"Alcocke"
|
1676
|
"blacksmith"
|
"William"
|
"Davidson"
|
1679
|
"bricklayer"
|
"Samuel"
|
"Richardson"
|
1683
|
"butcher"
|
"John"
|
"Dennis"
|
1676
|
"carpenter"
|
"John"
|
"Albert"
|
1675
|
"cabinet-maker"
|
"Robert"
|
"Avis"
|
1666
|
"joiner"
|
"Peter"
|
"Bartaboa"
|
1666
|
"chandler"
|
"William"
|
"Bates"
|
1674
|
"chyrurgeon"
|
"William"
|
"Axtell"
|
1674
|
"chyrurgeon"
|
"Thomas"
|
"Trapham"
|
1678
|
"combmaker"
|
"Paul"
|
"Bennett"
|
1673
|
"cooper"
|
"James"
|
"Hall"
|
1676
|
"cooper"
|
"Henry"
|
"Pullein"
|
1675
|
"cordwainer"
|
"George"
|
"Barnard"
|
1675
|
"cordwainer"
|
"Edward"
|
"Skannon"
|
1680
|
"cordwainer"
|
"John"
|
"Wilmott"
|
1675
|
"drugster"
|
"William"
|
"Mathews"
|
1682
|
"fisherman"
|
"Richard"
|
"Collingwood"
|
1674
|
"glazier"
|
"Thomas"
|
"Hudson"
|
1684
|
"goldsmith"
|
"Richard"
|
"Lord"
|
1677
|
"gunsmith"
|
"Stephen"
|
"Massey"
|
1664
|
"hatmaker"
|
"John"
|
"Rosewell"
|
1683
|
"ivory turner"
|
"William"
|
"Clifton"
|
1691
|
"labourer"
|
"John"
|
"Dennis"
|
1674
|
"limeburner"
|
"John"
|
"Hardwick"
|
1675
|
"mariner"
|
"Alexander"
|
"Bailing"
|
1680
|
"mariner"
|
"Thomas"
|
"Bowtell"
|
1675
|
"mariner"
|
"Peter"
|
"Claiton"
|
1675
|
"mariner"
|
"Joseph"
|
"Cupid"
|
1672
|
"mariner"
|
"Michael"
|
"Dunn"
|
1675
|
"mason"
|
"John"
|
"Stone"
|
1673
|
"merchant"
|
"John"
|
"Agard"
|
1680
|
"merchant"
|
"David Lopez"
|
"Narbona"
|
1674
|
"merchant"
|
"Abraham"
|
"Langford"
|
1675
|
"merchant"
|
"John"
|
"Sweeting"
|
1675
|
"merchant"
|
"Charles"
|
"Knight"
|
1680
|
"merchant"
|
"Cornelius"
|
"Vandananker"
|
1670
|
"merchant"
|
"Moses Jesurum"
|
"Cordova"
|
1675
|
"pewterer"
|
"Simon"
|
"Benning"
|
1667
|
"pipemaker"
|
"John"
|
"Pope"
|
1680
|
"porter"
|
"George"
|
"Paul"
|
1670
|
"poulterer"
|
"Richard"
|
"Jeffreys"
|
1677
|
"sailmaker"
|
"Adam"
|
"Brewer"
|
1671
|
"schoolmaster"
|
"Peter"
|
"Bird"
|
1677
|
"shipwright"
|
"William"
|
"Cavell"
|
1676
|
"tailor"
|
"William"
|
"Case"
|
1676
|
"tailor"
|
"Pewter"
|
"Ebden"
|
1683
|
"waterman"
|
"William"
|
"Brocke"
|
1674
|
"waterman"
|
"Joel"
|
"Clements"
|
1668
|
"wherryman"
|
"John"
|
"Grant"
|
1669
|
"victualler"
|
"Barnaby"
|
"Adams"
|
1675
|
"vintner"
|
"Gabriel"
|
"Adkins"
|
1668
|
"tavern-keeper"
|
"John"
|
"Baldwin"
|
1670
|
"tavern-keeper"
|
"Mary"
|
"Dayton"
|
1664
|
"tavern-keeper"
|
"James"
|
"Turpin"
|
1679
|
"tavern-keeper"
|
"Christopher"
|
"Mayham"
|
1664
|
Test me with "wait / wait / wait / wait / wait / wait / wait / wait / wait / wait".