| Chapter 11: Phrases |

| 11.3. Pattern matching

The following phrases all use different wording from each other:

> award 2 points
> award 8 points
> award 30 points

but they have the same meaning, with only one detail being different between them. A single definition (internal to Inform, since "award ... points" comes built in) covers these and all similar cases. It is written like so:

> To award (some - a number) points: ...

This pattern of words matches the text "award 4 points", and indeed "award four points", but not

> award "blue cheese" points;

which results in the following error:

> Problem. You wrote 'award "blue cheese" points' , but '"blue cheese"' seems to be some text, whereas I was expecting to find a number there.

The number does not need to be a literal constant such as 52, but can be anything which works out to be a number: the name of a number variable, for instance.

The bracketed part of the definition takes the form (name - what it is). The definition only applies if the text supplied agrees with the "what it is" part - for instance, 8 agreed with being a number, but "blue cheese" did not. If the definition does apply, then the name holds whatever value was supplied. For instance:

> To slam shut (box - an open container): say "With great panache, you slam shut [the box]."

The "what it is" will usually be a description of some objects, or the name of a kind of value, but it is also allowed to give a specific value. For instance, we could define:

> To award (some - 13) points: say "You shiver uncontrollably."

which would withhold this unlucky bounty, applying only to that one case. (As elsewhere in Inform, more specific definitions take priority over more general ones, so "award (some - 13) points" takes priority over "award (some - a number) points".)

Sometimes it will not be possible to tell if the value supplied meets the requirements until the game is actually playing. If, at run-time, no definition fits some phrase which has to be carried out, a run-time problem message is produced.

Finally, and more straightforwardly, we can specify variations in wording using slashes between alternative words in a "To ..." definition. For instance:

> To award (some - a number) point/points: ...

allows the final word to be either "point" or "points". Slashes like this can only be used with literal words (not bracketed values) and give alternative forms only of a single word at a time. (If we need more variation than that, we should make more than one definition.)

135
| Example Ahem Writing a phrase, with several variant forms, whose function is to follow a rule several times.

As we see in the example here, it is possible to use slashed variations in more than one place in a phrase, and to offer a number of separate forms. The main rule of thumb to remember is that value inputs for the phrase should always be separated by some text; so To do/follow (chosen rule - a rule) exactly/precisely/just/-- (N - a number) time/times: .... would cause a problem when we tried to call it with follow the throat-clearing rule 2 times. In general, we probably don't need to make our phrase definitions quite so flexible as this, but it's a good idea to account for "a" vs. "the", and for the possibility of using singular and plural forms, especially when writing extensions or other source to be shared. "Ahem" To do/follow (chosen rule - a rule) exactly/precisely/just (N - a number) time/times: repeat with index running from 1 to N begin; follow chosen rule; end repeat. This is the throat-clearing rule: say "'Ahem,' says [a random visible person who is not the player]." After waiting: do the throat-clearing rule just one time. Instead of listening: follow the throat-clearing rule precisely three times. Instead of smelling: follow the throat-clearing rule exactly 2 times. Chateau Marmont is a room. Tom, Jack, Zsa-Zsa, and Wilma-Faye are people in the Chateau. Zsa-Zsa and Wilma-Faye are women. Test me with "wait / smell / listen".

136
| Example Ferragamo Again Using the same phrase to produce different results with different characters.

Previous | Contents | Next