Difference between revisions of "Manual:DIL Manual/string in 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="esinsl"></span>
+
= string in stringlist =
−
string 'in' stringlist
+
'''Operator''' (integer).
−
    Argument 1: A string to find.
 
−
    Argument 2: A stringlist to search.
 
−
    Returns: 1.. if first string is found in stringlist, or 0 if it is
 
−
            non existent. If found the number equals the index of the
 
−
            string found plus one.
 
  
−
  Example 1:
+
  s in sl : integer
−
  s := "c";
 
−
  sl :=
 
−
;
 
−
  i := s in sl;
 
  
−
  The result of 'i' is 3, and sl.[i-1] equals "c" (s).
+
Looks for the string <code>s</code> in the stringlist <code>sl</code> and gives its position plus one: 1 for the first entry, 2 for the second, 0 when nothing matches. <code>sl.[(s in sl) - 1]</code> is the entry that matched.
  
−
  Example 2:
+
An entry matches when it is the whole of <code>s</code> or the first words of it. Case is ignored, spaces before <code>s</code> are skipped, and double spaces inside it count as one. So with <code>sl := {"red", "dark blue"}</code>:
 +
* <code>"red" in sl</code>, <code>"RED" in sl</code> and <code>"red apple" in sl</code> are 1;
 +
* <code>"dark blue" in sl</code> is 2;
 +
* <code>"re" in sl</code>, <code>"reddish" in sl</code> and <code>"dark" in sl</code> are 0.
  
−
<nowiki>
+
== Returns ==
 +
An integer: the index plus one of the first entry that matches, or 0.
  
−
  dilbegin foo();
+
== Notes ==
−
  code
+
* It is not a substring test. The entry must match from the start of <code>s</code> and end where a word of <code>s</code> ends: <code>"arrived" in {"arrived_port"}</code> is 0, but <code>"ship arrived" in {"ship"}</code> is 1.
−
dilend</nowiki>
+
* The first matching entry wins; an entry that is a shorter phrase than a later one hides it.
 +
* A string variable that was never assigned gives null instead of 0: an <code>if</code> treats it as false, but assigning it to an integer variable stops the program with a type error.
 +
* Fields that are stringlists work the same way: <code>"sword" in u.names</code> tells whether "sword" is one of the unit's names.
  
−
---~---~---~---~---~---~---~---~---
+
== Example ==
−
<span id="esinsl"></span>
+
<pre>
−
string 'in' stringlist
+
dilbegin string colourname(word : string);
−
    Argument 1: A string to find.
+
var
−
    Argument 2: A stringlist to search.
+
  sl : stringlist;
−
    Returns: 1.. if first string is found in stringlist, or 0 if it is
+
  i : integer;
−
            non existent. If found the number equals the index of the
+
code
−
            string found plus one.
+
{
 +
  sl := {"red", "green", "dark blue"};
 +
  i := word in sl;
 +
  if (i == 0)
 +
      return ("no colour");
 +
  return ("colour number " + itoa(i) + ": " + sl.[i - 1]);
 +
}
 +
dilend
 +
</pre>
 +
Tells which colour a word starts with: "Green grass" gives "colour number 2: green".
  
−
Example 1:
+
== See also ==
−
  s := "c";
+
* [[Manual:DIL Manual/string in string|string in string]]
−
  sl :=
+
* [[Manual:DIL Manual/string in extraptr|string in extraptr]]
−
;
+
* [[Manual:DIL Manual/in|in]]
−
  i := s in sl;
+
* [[Manual:DIL Manual/stringlist|stringlist]]
−
 
+
* [[Manual:DIL Manual/names|names]]
−
  The result of 'i' is 3, and sl.[i-1] equals "c" (s).
 
−
 
 
−
Example 2:
 
−
 
 
−
<nowiki>
 
−
 
 
−
  dilbegin foo();
 
−
  code
 
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:25, 14 November 2026

string in stringlist

Operator (integer).

s in sl : integer

Looks for the string s in the stringlist sl and gives its position plus one: 1 for the first entry, 2 for the second, 0 when nothing matches. sl.[(s in sl) - 1] is the entry that matched.

An entry matches when it is the whole of s or the first words of it. Case is ignored, spaces before s are skipped, and double spaces inside it count as one. So with sl := {"red", "dark blue"}:

  • "red" in sl, "RED" in sl and "red apple" in sl are 1;
  • "dark blue" in sl is 2;
  • "re" in sl, "reddish" in sl and "dark" in sl are 0.

Returns

An integer: the index plus one of the first entry that matches, or 0.

Notes

  • It is not a substring test. The entry must match from the start of s and end where a word of s ends: "arrived" in {"arrived_port"} is 0, but "ship arrived" in {"ship"} is 1.
  • The first matching entry wins; an entry that is a shorter phrase than a later one hides it.
  • A string variable that was never assigned gives null instead of 0: an if treats it as false, but assigning it to an integer variable stops the program with a type error.
  • Fields that are stringlists work the same way: "sword" in u.names tells whether "sword" is one of the unit's names.

Example

dilbegin string colourname(word : string);
var
   sl : stringlist;
   i : integer;
code
{
   sl := {"red", "green", "dark blue"};
   i := word in sl;
   if (i == 0)
      return ("no colour");
   return ("colour number " + itoa(i) + ": " + sl.[i - 1]);
}
dilend

Tells which colour a word starts with: "Green grass" gives "colour number 2: green".

See also