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

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="bpsubs"></span>
+
= substring() =
−
  substring ( var l : stringlist, s : string )
+
'''Procedure.''' Removes a string from a stringlist.
−
    l : Stringlist to remove string from.
+
 
−
    s : String to remove
+
  substring(sl : stringlist, s : string)
−
    result: Removes string 's' from stringlist 'l'.
+
 
−
<span id="bpsubs"></span>
+
Removes the first element of <code>sl</code> that matches <code>s</code>, ignoring case. Only one element goes per call. <code>sl</code> is a stringlist variable or a stringlist field, such as a unit's <code>names</code>. Despite its name, it has nothing to do with parts of a string; for those see <code>left()</code>, <code>mid()</code> and <code>right()</code>.
−
substring ( var l : stringlist, s : string )
+
 
−
    l : Stringlist to remove string from.
+
== Parameters ==
−
    s : String to remove
+
{| class="wikitable"
−
    result: Removes string 's' from stringlist 'l'.
+
! Name !! Type !! Meaning
 +
|-
 +
| sl || stringlist || The list: a variable or a field.
 +
|-
 +
| s || string || The string to remove.
 +
|}
 +
 
 +
== Notes ==
 +
* An element matches when it is <code>s</code>, or the first word or words of <code>s</code>: with <code>{"ship", "boat"}</code>, <code>substring(sl, "ship arrived")</code> removes <code>"ship"</code>. An element longer than <code>s</code> doesn't match: <code>"ship"</code> doesn't remove <code>"ship arrived"</code>.
 +
* When nothing matches, or <code>s</code> fails, the list stays as it is.
 +
* To remove every copy, loop while <code>s in sl</code>.
 +
* <code>remove()</code> removes an element by its place in the list.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin greeter();
 +
var
 +
  seen : stringlist;
 +
code
 +
{
 +
  :start:
 +
  wait(SFB_CMD, (activator.type == UNIT_ST_PC) and (command("say") or command("quit")));
 +
  if (command("quit"))
 +
      substring(seen, activator.name);
 +
  else if (not (activator.name in seen))
 +
  {
 +
      addstring(seen, activator.name);
 +
      exec("say Welcome, " + activator.name + "!", self);
 +
  }
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
An NPC that greets each player once, and forgets them when they quit.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/addstring()|addstring()]]
 +
* [[Manual:DIL Manual/remove()|remove()]]
 +
* [[Manual:DIL Manual/in|in]]
 +
* [[Manual:DIL Manual/stringlist|stringlist]]
 +
* [[Manual:DIL Manual/names|names]]

Latest revision as of 23:23, 14 November 2026

substring()

Procedure. Removes a string from a stringlist.

substring(sl : stringlist, s : string)

Removes the first element of sl that matches s, ignoring case. Only one element goes per call. sl is a stringlist variable or a stringlist field, such as a unit's names. Despite its name, it has nothing to do with parts of a string; for those see left(), mid() and right().

Parameters

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

Notes

  • An element matches when it is s, or the first word or words of s: with {"ship", "boat"}, substring(sl, "ship arrived") removes "ship". An element longer than s doesn't match: "ship" doesn't remove "ship arrived".
  • When nothing matches, or s fails, the list stays as it is.
  • To remove every copy, loop while s in sl.
  • remove() removes an element by its place in the list.

Example

dilbegin greeter();
var
   seen : stringlist;
code
{
   :start:
   wait(SFB_CMD, (activator.type == UNIT_ST_PC) and (command("say") or command("quit")));
   if (command("quit"))
      substring(seen, activator.name);
   else if (not (activator.name in seen))
   {
      addstring(seen, activator.name);
      exec("say Welcome, " + activator.name + "!", self);
   }
   goto start;
}
dilend

An NPC that greets each player once, and forgets them when they quit.

See also