| 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.

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

Here we use phrases that match individual items where possible, and the general kind otherwise: "Ferragamo Again" The Break Room is a room. Vanessa, Tina, and Lisa are women in the Break Room. Mark and Holman are men in the Break Room. Understand the commands "ask" and "tell" and "answer" as something new. Understand "talk about [any subject]" as talking about. Talking about is an action applying to one thing. Understand "talk about [text]" as talking randomly about. Talking randomly about is an action applying to one topic. Carry out talking randomly about: say "Mostly you're interested in [the list of subjects]." Carry out talking about something: change the previous subject to the noun. Report talking about something: say "You chat for a while about [the noun]." A subject is a kind of thing. Assyrian vowel sounds, designer handbags, and instant run-off voting are subjects. Understand "linguistics" and "mute" and "stop" as sounds. Understand "prada" and "tods" and "coach" and "carmen marc valvo" as designer handbags. Understand "reform" and "election" and "election fraud" and "two-party system" and "Diebold" as instant run-off voting. To say (annoyed-person - a person) gestures in irritation: say "[The annoyed-person] sighs heavily. [run paragraph on]" To say (annoyed-person - Vanessa) gestures in irritation: say "[The annoyed-person] takes off her glasses and polishes them on her sleeve. [run paragraph on]". To say (annoyed-person - Holman) gestures in irritation: say "Holman bobs his head. [run paragraph on]" The previous subject is a subject that varies. Instead of talking about something for more than one turn: if the noun is the previous subject, say "[a random visible person gestures in irritation]Maybe you should let this one go.[line break][paragraph break]"; otherwise continue the action. Test me with "talk about chocolate / talk about vowel sounds / g / talk about handbags / talk about prada / talk about tods".

Previous | Contents | Next