Manual:DIL Manual/substring()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:23, 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

substring()

Procedure. Removes a string from a stringlist.

substring(sl : stringlist, s : string)

Removes the first element of sl that matches s, ignoring case. Only one element goes per call. sl is a stringlist variable or a stringlist field, such as a unit's names. Despite its name, it has nothing to do with parts of a string; for those see left(), mid() and right().

Parameters

Name Type Meaning
sl stringlist The list: a variable or a field.
s string The string to remove.

Notes

  • An element matches when it is s, or the first word or words of s: with {"ship", "boat"}, substring(sl, "ship arrived") removes "ship". An element longer than s doesn't match: "ship" doesn't remove "ship arrived".
  • When nothing matches, or s fails, the list stays as it is.
  • To remove every copy, loop while s in sl.
  • remove() removes an element by its place in the list.

Example

dilbegin greeter();
var
   seen : stringlist;
code
{
   :start:
   wait(SFB_CMD, (activator.type == UNIT_ST_PC) and (command("say") or command("quit")));
   if (command("quit"))
      substring(seen, activator.name);
   else if (not (activator.name in seen))
   {
      addstring(seen, activator.name);
      exec("say Welcome, " + activator.name + "!", self);
   }
   goto start;
}
dilend

An NPC that greets each player once, and forgets them when they quit.

See also