Manual:DIL Manual/string in stringlist
Jump to navigation
Jump to search
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 sland"red apple" in slare 1;"dark blue" in slis 2;"re" in sl,"reddish" in sland"dark" in slare 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
sand end where a word ofsends:"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
iftreats 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.namestells 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".