Difference between revisions of "Manual:DIL Manual/addstring()"

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="bpadds"></span>
+
= addstring() =
−
addstring ( var l : stringlist, s : string )
+
'''Procedure.''' Adds a string to the end of a stringlist.
−
    l : Stringlist to add string to.
 
−
    s : String to add.
 
−
    result : Adds a string 's' to a stringlist 'l'.
 
  
−
---~---~---~---~---~---~---~---~---
+
  addstring(sl : stringlist, s : string)
−
<span id="bpadds"></span>
 
−
  addstring ( var l : stringlist, s : string )
 
−
    l : Stringlist to add string to.
 
−
    s : String to add.
 
−
    result : Adds a string 's' to a stringlist 'l'.
 
  
−
---~---~---~---~---~---~---~---~---
+
Adds <code>s</code> as the new last element of <code>sl</code>. <code>sl</code> is a stringlist variable or a stringlist field, such as a unit's <code>names</code> or an extra description's <code>names</code>.
 +
 
 +
== Parameters ==
 +
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| sl || stringlist || The list: a variable or a field.
 +
|-
 +
| s || string || The string to add.
 +
|}
 +
 
 +
== Notes ==
 +
* Spaces at the start and end are removed, and a row of spaces inside becomes a single space. A string that starts with a space is also logged ("DIL ... addstring [...] has whitespace").
 +
* It adds the string even when the list has it already. Test with <code>in</code> first to keep a list without repeats.
 +
* When <code>s</code> fails (a field of a null unit), nothing is added.
 +
* <code>sl := null;</code> empties a list (quests.zon does this before building a new one). <code>sl := {};</code> doesn't compile.
 +
* A list constant <code>{...}</code> takes only constants; a list with values from variables is built with <code>addstring</code>.
 +
* <code>insert()</code> adds at a given place; <code>substring()</code> and <code>remove()</code> take elements out.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin who_is_here();
 +
var
 +
  u : unitptr;
 +
  sl : stringlist;
 +
  i : integer;
 +
code
 +
{
 +
  foreach (UNIT_ST_PC, u)
 +
      addstring(sl, u.name);
 +
 
 +
  i := 0;
 +
  while (i < length(sl))
 +
  {
 +
      sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", self);
 +
      i := i + 1;
 +
  }
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
Builds a list of the players in the room and shows it, numbered.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/substring()|substring()]]
 +
* [[Manual:DIL Manual/insert()|insert()]]
 +
* [[Manual:DIL Manual/remove()|remove()]]
 +
* [[Manual:DIL Manual/in|in]]
 +
* [[Manual:DIL Manual/length()|length()]]
 +
* [[Manual:DIL Manual/split()|split()]]
 +
* [[Manual:DIL Manual/stringlist|stringlist]]
 +
* [[Manual:DIL Manual/names|names]]

Latest revision as of 23:23, 14 November 2026

addstring()

Procedure. Adds a string to the end of a stringlist.

addstring(sl : stringlist, s : string)

Adds s as the new last element of sl. sl is a stringlist variable or a stringlist field, such as a unit's names or an extra description's names.

Parameters

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

Notes

  • Spaces at the start and end are removed, and a row of spaces inside becomes a single space. A string that starts with a space is also logged ("DIL ... addstring [...] has whitespace").
  • It adds the string even when the list has it already. Test with in first to keep a list without repeats.
  • When s fails (a field of a null unit), nothing is added.
  • sl := null; empties a list (quests.zon does this before building a new one). sl := {}; doesn't compile.
  • A list constant {...} takes only constants; a list with values from variables is built with addstring.
  • insert() adds at a given place; substring() and remove() take elements out.

Example

dilbegin who_is_here();
var
   u : unitptr;
   sl : stringlist;
   i : integer;
code
{
   foreach (UNIT_ST_PC, u)
      addstring(sl, u.name);

   i := 0;
   while (i < length(sl))
   {
      sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", self);
      i := i + 1;
   }
   quit;
}
dilend

Builds a list of the players in the room and shows it, numbered.

See also