Manual:DIL Manual/stringlist

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

stringlist

Type. An ordered list of strings.

var
   sl : stringlist;

Stringlists hold names, keywords and lists of words: a unit's names, an extra's names, the result of split() or getwords(). A list is written as strings in braces: {"red", "green", "blue"}.

A stringlist variable holds its own list. Assigning a list to it (sl := u.names;) copies the strings, and a template that gets a stringlist argument gets a copy as well.

Working with a stringlist

  • sl.[i] is the string at position i, counting from 0. It can be assigned to: sl.[0] := "black";.
  • length(sl) is the number of strings.
  • s in sl is the position plus one of the first entry that is s, or the first whole words of s, ignoring case; 0 if there is none. "ship arrived" in {"ship"} is 1, but "ship" in {"ship arrived"} and "arrived" in {"arrived_port"} are 0.
  • addstring(sl, s) adds at the end, substring(sl, s) takes a string out, insert(sl, i, s) and remove(sl, i) work by position.
  • sl.[i].[j] is a character of one of the strings.

Notes

  • {...} may hold only constants: strings and macros that become strings. {name} with a variable is a syntax error; build such a list with addstring().
  • sl := {}; doesn't compile ("Assigning incompatible types (C)"). An empty {} only works as an argument in a zone file's dilcopy. To empty a list, assign null: sl := null;.
  • Reading sl.[i] outside the list fails and writes "index of stringlist out of bounds" to the zone log; assigning outside it does nothing.
  • Stringlist variables keep their value through waits and are saved with the unit.
  • A stringlist can be the argument of a template copied with dilcopy in a zone file, but not of the dilcopy() procedure, which only passes strings and integers.

Example

dilbegin list_names(u : unitptr);
var
   sl : stringlist;
   i : integer;
   s : string;
code
{
   sl := u.names;
   s := "";
   while (i < length(sl))
   {
      s := s + "[" + sl.[i] + "] ";
      i := i + 1;
   }
   sendtext(u.name + " answers to: " + s + "<br/>", self);
   if ("dragon" in sl)
      sendtext("Careful, that is a dragon.<br/>", self);
   quit;
}
dilend

Shows a unit's names one by one, and warns if one of them is "dragon".

See also