Inform 7 Home Page / Documentation
§11.11. Repeat running through
Inform is not used very much for numerical work, so the kind of repeat loop described in the previous section is not much used. Inform's natural domain is really the world of things and rooms, so the following kind of repeat is much more useful.
repeat with (a name not so far used) running through (description of values):
This phrase causes the block of phrases following it to be repeated once for each value matching the description, storing that value in the named variable. (The variable exists only temporarily, within the repetition.) Example:
If there are no containers, or they are all closed, the phrases will not be followed at all. Inform will issue a Problem message if the range of the loop may be infinite: for example, it won't allow:
On the other hand it will allow:
which repeats 1440 times, starting with T at midnight and finishing at 11:59 PM. See the Kinds index for which kinds of value can be repeated through.
As with counting the "number of ..." objects satisfying some property, we can run through a wide variety of possibilities - any description whose range is possible for Inform to search. For example:
repeat with dinner guest running through the people in the Dining Room:
repeat with possession running through things carried:
repeat with event running through non-recurring scenes which are happening:
The following lists the whereabouts of all men in lighted rooms:
repeat with suspect running through the men who are in a lighted room:
say "[The suspect] is in [the location of the suspect].";
One small note of caution: if what the "repeat" loop does is to change the things being repeated through, changing in particular whether items not yet reached will qualify to be repeated through, the results can be unexpected. Rather than writing "repeat with X running through D", it may be safer to try "while there is D (called X)", though note that this will only finish if X is always changed so that it no longer qualifies.
Many simple repetitions can effectively be done with a "now..." instruction: it is quicker to say
Repeat comes in handy when we have something a bit more complicated to do with each item:
repeat with dancer running through people who are not the player:
if dancer is alert:
now dancer is occupied;
let partner be a random alert person who is not the dancer;
if partner is a person:
now partner is occupied;
say "[The dancer] [the current round][if a random chance of 1 in 5 succeeds] [a random manner][end if] with [partner]. ";
say "[paragraph break][The dancer] is forced to be a wallflower. Poor [dancer]. ";
say paragraph break.
Notice we did not say "repeat with dancer running through alert people who are not the player...". This is because Inform would draw up a list of alert people at the beginning of the repeat, and not take into account which people became occupied partway through the repetition. If we want to make sure that each person dances only with one other person, we have to continue checking alertness each time we run through the repetition.
After all the partners are assigned, we can set up for the next turn by making everyone alert again, and for this we do not need "repeat":
The Pacific Ballroom is a room. "A rather utilitarian space at the moment, since this is a class and not a party." Timmy, Tommy, Joey, George, Mary, Martha, Yvette, McQueen, Linus, and Patricia are people in the Pacific Ballroom.