Difference between revisions of "Manual:DIL Manual/in"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
| + | = in = | ||
| + | '''Keyword''' (operator; integer or extraptr). Looks for a string in a string, a stringlist or a list of extras. | ||
| + | s in text : integer (text : string) | ||
| + | s in list : integer (list : stringlist) | ||
| + | s in extras : extraptr (extras : extraptr) | ||
| − | < | + | The left side is always a string. What <code>in</code> does depends on the right side: |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | * '''string''': TRUE (1) if <code>s</code> occurs anywhere in <code>text</code>, ignoring case, otherwise FALSE (0). <code>"key" in "Do you have a KEY?"</code> is TRUE, and so is <code>"spook" in "nospook"</code>. | |
| + | * '''stringlist''': the position of the first entry that matches, plus one (so 1 for the first entry), or 0 if none does. An entry matches when it is <code>s</code>, or the first word or words of <code>s</code>, ignoring case: <code>"ship arrived" in {"ship"}</code> is 1, but <code>"arrived" in {"arrived_port"}</code> is 0. | ||
| + | * '''extraptr''' (such as <code>u.extra</code> or <code>pc.quests</code>): the first extra that has <code>s</code> as one of its names (the whole name, ignoring case), or null. | ||
| + | |||
| + | == Returns == | ||
| + | An integer for a string or stringlist, an extraptr for extras. Both work directly in an <code>if</code>: 0 and null are false. If either side fails (a field of a null unit, say), the result fails, which is also false. | ||
| + | |||
| + | == Notes == | ||
| + | * <code>in</code> binds like <code>*</code>, <code>/</code> and <code>and</code>: tighter than <code>+</code>, <code>-</code>, <code>or</code> and comparisons, looser than <code>not</code>. So write <code>not ("x" in sl)</code>: <code>not "x" in sl</code> is read as <code>(not "x") in sl</code> and vmc stops with "Arg 1 of 'in' not string". Likewise <code>"a" + b in s</code> means <code>"a" + (b in s)</code>. | ||
| + | * The string test is a substring test. To match a whole word, split the text into words first (<code>getwords()</code>) and use the stringlist form. | ||
| + | * In the stringlist form, leading spaces in <code>s</code> are skipped and runs of spaces count as one. | ||
| + | * A list in braces works on the right: <code>arg in {":brief:", ":chars:"}</code> (baselook.zon). The braces take only constants, not variables. | ||
| + | * For quests, use quests.zon's functions (<code>qstOngoing@quests</code> and friends) rather than testing <code>pc.quests</code> by hand. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin gate_talk(); | ||
| + | var | ||
| + | ex : extraptr; | ||
| + | n : integer; | ||
| + | code | ||
| + | { | ||
| + | :start: | ||
| + | wait(SFB_CMD, command("say") and (activator.type == UNIT_ST_PC)); | ||
| + | |||
| + | if (not ("password" in argument)) | ||
| + | goto start; | ||
| + | |||
| + | n := getword(argument) in {"swordfish", "dragon"}; | ||
| + | ex := "$gate_pass" in activator.extra; | ||
| + | |||
| + | if (n == 0) | ||
| + | act("$1n shakes $1s head.", A_ALWAYS, self, null, activator, TO_ROOM); | ||
| + | else if (ex != null) | ||
| + | act("$1n nods to $3n: 'Welcome back.'", A_ALWAYS, self, null, activator, TO_ROOM); | ||
| + | else | ||
| + | { | ||
| + | addextra(activator.extra, {"$gate_pass"}, "1"); | ||
| + | act("$1n lets $3n through.", A_ALWAYS, self, null, activator, TO_ROOM); | ||
| + | } | ||
| + | goto start; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | A guard who listens for "password" anywhere in what is said (string form), checks the first word against the passwords (stringlist form) and remembers who has been let through (extra form). | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/Operators|Operators]] | ||
| + | * [[Manual:DIL Manual/getwords()|getwords()]] | ||
| + | * [[Manual:DIL Manual/extra|extra]] | ||
| + | * [[Manual:DIL Manual/quests|quests]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
| + | * [[Manual:DIL Manual/extraptr|extraptr]] | ||
Latest revision as of 23:14, 14 November 2026
Contents
in
Keyword (operator; integer or extraptr). Looks for a string in a string, a stringlist or a list of extras.
s in text : integer (text : string) s in list : integer (list : stringlist) s in extras : extraptr (extras : extraptr)
The left side is always a string. What in does depends on the right side:
- string: TRUE (1) if
soccurs anywhere intext, ignoring case, otherwise FALSE (0)."key" in "Do you have a KEY?"is TRUE, and so is"spook" in "nospook". - stringlist: the position of the first entry that matches, plus one (so 1 for the first entry), or 0 if none does. An entry matches when it is
s, or the first word or words ofs, ignoring case:"ship arrived" in {"ship"}is 1, but"arrived" in {"arrived_port"}is 0. - extraptr (such as
u.extraorpc.quests): the first extra that hassas one of its names (the whole name, ignoring case), or null.
Returns
An integer for a string or stringlist, an extraptr for extras. Both work directly in an if: 0 and null are false. If either side fails (a field of a null unit, say), the result fails, which is also false.
Notes
inbinds like*,/andand: tighter than+,-,orand comparisons, looser thannot. So writenot ("x" in sl):not "x" in slis read as(not "x") in sland vmc stops with "Arg 1 of 'in' not string". Likewise"a" + b in smeans"a" + (b in s).- The string test is a substring test. To match a whole word, split the text into words first (
getwords()) and use the stringlist form. - In the stringlist form, leading spaces in
sare skipped and runs of spaces count as one. - A list in braces works on the right:
arg in {":brief:", ":chars:"}(baselook.zon). The braces take only constants, not variables. - For quests, use quests.zon's functions (
qstOngoing@questsand friends) rather than testingpc.questsby hand.
Example
dilbegin gate_talk();
var
ex : extraptr;
n : integer;
code
{
:start:
wait(SFB_CMD, command("say") and (activator.type == UNIT_ST_PC));
if (not ("password" in argument))
goto start;
n := getword(argument) in {"swordfish", "dragon"};
ex := "$gate_pass" in activator.extra;
if (n == 0)
act("$1n shakes $1s head.", A_ALWAYS, self, null, activator, TO_ROOM);
else if (ex != null)
act("$1n nods to $3n: 'Welcome back.'", A_ALWAYS, self, null, activator, TO_ROOM);
else
{
addextra(activator.extra, {"$gate_pass"}, "1");
act("$1n lets $3n through.", A_ALWAYS, self, null, activator, TO_ROOM);
}
goto start;
}
dilend
A guard who listens for "password" anywhere in what is said (string form), checks the first word against the passwords (stringlist form) and remembers who has been let through (extra form).