Difference between revisions of "Manual:DIL Manual/stringlist"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = stringlist = | |
| − | + | '''Type.''' An ordered list of strings. | |
| − | + | var | |
| − | + | sl : stringlist; | |
| − | |||
| − | |||
| − | + | Stringlists hold names, keywords and lists of words: a unit's <code>names</code>, an extra's <code>names</code>, the result of <code>split()</code> or <code>getwords()</code>. A list is written as strings in braces: <code>{"red", "green", "blue"}</code>. | |
| − | + | A stringlist variable holds its own list. Assigning a list to it (<code>sl := u.names;</code>) copies the strings, and a template that gets a stringlist argument gets a copy as well. | |
| − | |||
| − | |||
| − | + | == Working with a stringlist == | |
| + | * <code>sl.[i]</code> is the string at position <code>i</code>, counting from 0. It can be assigned to: <code>sl.[0] := "black";</code>. | ||
| + | * <code>length(sl)</code> is the number of strings. | ||
| + | * <code>s in sl</code> is the position plus one of the first entry that is <code>s</code>, or the first whole words of <code>s</code>, ignoring case; 0 if there is none. <code>"ship arrived" in {"ship"}</code> is 1, but <code>"ship" in {"ship arrived"}</code> and <code>"arrived" in {"arrived_port"}</code> are 0. | ||
| + | * <code>addstring(sl, s)</code> adds at the end, <code>substring(sl, s)</code> takes a string out, <code>insert(sl, i, s)</code> and <code>remove(sl, i)</code> work by position. | ||
| + | * <code>sl.[i].[j]</code> is a character of one of the strings. | ||
| − | + | == Notes == | |
| − | + | * <code>{...}</code> may hold only constants: strings and macros that become strings. <code>{name}</code> with a variable is a syntax error; build such a list with <code>addstring()</code>. | |
| − | + | * <code>sl := {};</code> doesn't compile ("Assigning incompatible types (C)"). An empty <code>{}</code> only works as an argument in a zone file's <code>dilcopy</code>. To empty a list, assign <code>null</code>: <code>sl := null;</code>. | |
| − | + | * Reading <code>sl.[i]</code> 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 <code>dilcopy</code> in a zone file, but not of the <code>dilcopy()</code> procedure, which only passes strings and integers. | ||
| − | + | == Example == | |
| − | + | <pre> | |
| + | 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 | ||
| + | </pre> | ||
| + | Shows a unit's names one by one, and warns if one of them is "dragon". | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/string|string]] | |
| − | + | * [[Manual:DIL Manual/intlist|intlist]] | |
| − | + | * [[Manual:DIL Manual/addstring()|addstring()]] | |
| − | + | * [[Manual:DIL Manual/substring()|substring()]] | |
| − | + | * [[Manual:DIL Manual/insert()|insert()]] | |
| − | + | * [[Manual:DIL Manual/remove()|remove()]] | |
| − | + | * [[Manual:DIL Manual/length()|length()]] | |
| − | + | * [[Manual:DIL Manual/in|in]] | |
| − | + | * [[Manual:DIL Manual/split()|split()]] | |
| − | + | * [[Manual:DIL Manual/getwords()|getwords()]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:27, 14 November 2026
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".