Difference between revisions of "Manual:DIL Manual/in"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="ein"></span>
+
= in =
−
'''in:'''
+
'''Keyword''' (operator; integer or extraptr). Looks for a string in a string, a stringlist or a list of extras.
−
The special operator 'in' is a multipurpose operator for
 
−
a lot of things. It allows you to search through
 
−
quests and extra descriptions, stringlists and search
 
−
for words in strings.
 
  
−
  ---~---~---~---~---~---~---~---~---
+
  s in text      : integer  (text : string)
−
<span id="ein"></span>
+
  s in list      : integer  (list : stringlist)
−
  '''in:'''
+
s in extras    : extraptr  (extras : extraptr)
−
The special operator 'in' is a multipurpose operator for
 
−
a lot of things. It allows you to search through
 
−
quests and extra descriptions, stringlists and search
 
−
for words in strings.
 
  
−
---~---~---~---~---~---~---~---~---
+
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

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 s occurs anywhere in text, 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 of s, ignoring case: "ship arrived" in {"ship"} is 1, but "arrived" in {"arrived_port"} is 0.
  • extraptr (such as u.extra or pc.quests): the first extra that has s 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 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

  • in binds like *, / and and: tighter than +, -, or and comparisons, looser than not. So write not ("x" in sl): not "x" in sl is read as (not "x") in sl and vmc stops with "Arg 1 of 'in' not string". Likewise "a" + b in s means "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 s are 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@quests and friends) rather than testing pc.quests by 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).

See also