Manual:DIL Manual/stringlist
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 positioni, counting from 0. It can be assigned to:sl.[0] := "black";.length(sl)is the number of strings.s in slis the position plus one of the first entry that iss, or the first whole words ofs, 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)andremove(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 withaddstring().sl := {};doesn't compile ("Assigning incompatible types (C)"). An empty{}only works as an argument in a zone file'sdilcopy. To empty a list, assignnull: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
dilcopyin a zone file, but not of thedilcopy()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".