Chapter 19: Advanced Text
19.8. Replacements

Suppose V is an indexed text which varies - perhaps a property of something, or a variable defined everywhere, or a temporary "let"-named value. How do we change its contents? The easiest way is simply to assign text to it. Thus:

let V be "It is now [the time of the day in words]."

And, for instance,

let V be "[V]!"

adds an exclamation mark at the end of V.

Otherwise, it is more useful (also a little faster) to modify V by changing its characters, words and so on. Thus:

replace character number N in V with NEW

changes the Nth character to NEW (which is not required to be a single character: it simply occupies the spot previously occupied by the Nth character). Similarly:

replace word number N in V with NEW
replace punctuated word number N in V with NEW
replace unpunctuated word number N in V with NEW
replace line number N in V with NEW
replace paragraph number N in V with NEW

Last, but not least, we can replace text wherever it occurs:

replace the text FIND in V with NEW
replace the text FIND in V with NEW, case insensitively
replace the regular expression FIND in V with NEW
replace the regular expression FIND in V with NEW, case insensitively

These match as much as they can, but again, with the matches not allowed to overlap. Thus

replace the text "a" in V with "z"

changes every lower-case "a" to "z": the same thing done case insensitively would change each "a" or "A" to "z".

When replacing a regular expression, the replacement text also has a few special meanings (though, thankfully, many fewer than for the expression itself). Once again "\n" and "\t" can be used for line break and tab characters, and "\\" must be used for an actual backslash. But, very usefully, "\1" to "\9" expand as the contents of groups numbered 1 to 9, and "\0" to the exact text matched. So:

replace the regular expression "\d+" in V with "roughly \0"

adds the word "roughly" in front of any run of digits in V, because \0 becomes in turn whichever run of digits matched. And

replace the regular expression "(\w+) (.*)" in V with "\2, \1"

performs the transformation "Frank Booth" to "Booth, Frank".

Finally, prefixing the number by "l" or "u" forces the text it represents into lower or upper case, respectively. For instance:

replace the regular expression "\b(\w)(\w*)" in X with "\u1\l2";

changes the casing of X to "title casing", where each individual word is capitalised. (This is a little slow on large texts, since so many matches and replacements are made: it's more efficient to use the official phrases for changing case.)


395
* Example  Blackout
Filtering the names of rooms printed while in darkness.

RB
396
* Example  Igpay Atinlay
A pig Latin filter for the player's commands.

RB
397
* Example  Fido
A dog the player can name and un-name at will.

RB
398
** Example  Northstar
Making Inform understand ASK JOSH TO TAKE INVENTORY as JOSH, TAKE INVENTORY. This requires us to use a regular expression on the player's command, replacing some of the content.

RB

Most of the time, Inform understands commands to other characters when they take the form "JOSH, TAKE INVENTORY" or "JOAN, WEAR THE ARMOR". But novice players might also try commands of the form ASK JOSH TO TAKE INVENTORY or ORDER JOAN TO WEAR THE ARMOR.

The easiest way to make Inform understand such commands is to meddle directly with the player's command, changing it into the format that the game will understand, as here:

"Northstar"

The Northstar Cafe is a room. "The Northstar is crammed with its usual brunch crowd, and you were lucky to get a table at all. You are now awaiting the arrival of your ricotta pancakes."

Josh is a man in The Northstar Cafe. "Josh is on his way past your table." The description of Josh is "He is a waiter here, but you also know him socially, so he tends to be more chatty than the other waiters." A persuasion rule: persuasion succeeds.

After reading a command:
    let N be indexed text;
    let N be the player's command;
    replace the regular expression "(ask|tell|order) (.+) to (.+)" in N with "\2, \3";
    change the text of the player's command to N.

Test me with "ask Josh to take inventory / tell Josh to take inventory / order Josh to take inventory".

Note that we have to copy N back explicitly to replace the player's command.

399
** Example  Mr. Burns' Repast
Letting the player guess types for an unidentifiable fish.

RB
400
*** Example  Cave-troll
Determining that the command the player typed is invalid, editing it, and re-examining it to see whether it now reads correctly.

RB


PreviousContentsNext