Difference between revisions of "Manual:DIL Manual/stringlist"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="strl"></span>
+
= stringlist =
−
'''Stringlist:'''
+
'''Type.''' An ordered list of strings.
  
−
  A stringlist is a list of separate strings. This is used for things such as
+
  var
−
  (multiple) names or keywords. You may request a specified word in a stringlist
+
    sl : stringlist;
−
by its number.
 
−
'''Example:'''
 
  
−
  mystring := self.names.[2];
+
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>.
  
−
  Returning null if out of bounds of the stringlist (see 'length()'). Static
+
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.
−
stringlists are defined just as in the rest of the zonefile, as a comma separated
 
−
list of static strings within curly brackets.
 
  
−
'''Example:'''
+
== 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.
  
−
  mysrtringlist :=
+
== Notes ==
−
Stringlists are modified through the 'addstring()' and 'substring()' procedures.
+
* <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>.
−
Stringlists are searched easily by the 'in' operator. See documentation below.
+
* <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>.
−
They can also be set directly (see example above).  Variables of type string are
+
* 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.
−
saved with DIL programs, if attached to a saved unit.
+
* 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.
  
−
   Elements of each separate string in a stringlist can be accessed by appending
+
== Example ==
−
a separate position at the end of the request for a string as follows:
+
<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".
  
−
'''Example'''
+
== See also ==
−
if (strlist.[5].[3]=="b")
+
* [[Manual:DIL Manual/string|string]]
−
'''Note'''
+
* [[Manual:DIL Manual/intlist|intlist]]
−
  See the strings for more information on accessing a single element.
+
* [[Manual:DIL Manual/addstring()|addstring()]]
−
 
+
* [[Manual:DIL Manual/substring()|substring()]]
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/insert()|insert()]]
−
<span id="strl"></span>
+
* [[Manual:DIL Manual/remove()|remove()]]
−
'''Stringlist:'''
+
* [[Manual:DIL Manual/length()|length()]]
−
 
+
* [[Manual:DIL Manual/in|in]]
−
  A stringlist is a list of separate strings. This is used for things such as
+
* [[Manual:DIL Manual/split()|split()]]
−
(multiple) names or keywords. You may request a specified word in a stringlist
+
* [[Manual:DIL Manual/getwords()|getwords()]]
−
by its number.
 
−
'''Example:'''
 
−
 
 
−
  mystring := self.names.[2];
 
−
 
 
−
  Returning null if out of bounds of the stringlist (see 'length()'). Static
 
−
stringlists are defined just as in the rest of the zonefile, as a comma separated
 
−
list of static strings within curly brackets.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  mysrtringlist :=
 
−
Stringlists are modified through the 'addstring()' and 'substring()' procedures.
 
−
Stringlists are searched easily by the 'in' operator. See documentation below.
 
−
They can also be set directly (see example above).  Variables of type string are
 
−
saved with DIL programs, if attached to a saved unit.
 
−
 
 
−
  Elements of each separate string in a stringlist can be accessed by appending
 
−
a separate position at the end of the request for a string as follows:
 
−
 
 
−
'''Example'''
 
−
if (strlist.[5].[3]=="b")
 
−
'''Note'''
 
−
  See the strings for more information on accessing a single element.
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

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 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