Manual:DIL Manual/string in stringlist

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:25, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

string in stringlist

Operator (integer).

s in sl : integer

Looks for the string s in the stringlist sl and gives its position plus one: 1 for the first entry, 2 for the second, 0 when nothing matches. sl.[(s in sl) - 1] is the entry that matched.

An entry matches when it is the whole of s or the first words of it. Case is ignored, spaces before s are skipped, and double spaces inside it count as one. So with sl := {"red", "dark blue"}:

  • "red" in sl, "RED" in sl and "red apple" in sl are 1;
  • "dark blue" in sl is 2;
  • "re" in sl, "reddish" in sl and "dark" in sl are 0.

Returns

An integer: the index plus one of the first entry that matches, or 0.

Notes

  • It is not a substring test. The entry must match from the start of s and end where a word of s ends: "arrived" in {"arrived_port"} is 0, but "ship arrived" in {"ship"} is 1.
  • The first matching entry wins; an entry that is a shorter phrase than a later one hides it.
  • A string variable that was never assigned gives null instead of 0: an if treats it as false, but assigning it to an integer variable stops the program with a type error.
  • Fields that are stringlists work the same way: "sword" in u.names tells whether "sword" is one of the unit's names.

Example

dilbegin string colourname(word : string);
var
   sl : stringlist;
   i : integer;
code
{
   sl := {"red", "green", "dark blue"};
   i := word in sl;
   if (i == 0)
      return ("no colour");
   return ("colour number " + itoa(i) + ": " + sl.[i - 1]);
}
dilend

Tells which colour a word starts with: "Green grass" gives "colour number 2: green".

See also