Difference between revisions of "Manual:DIL Manual/string in stringlist"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
| + | = string in stringlist = | ||
| + | '''Operator''' (integer). | ||
| + | s in sl : integer | ||
| − | < | + | 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. |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | 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. | |
| − | + | == 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 <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. | ||
| + | * 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 == | |
| + | <pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | Tells which colour a word starts with: "Green grass" gives "colour number 2: green". | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/string in string|string in string]] | |
| − | + | * [[Manual:DIL Manual/string in extraptr|string in extraptr]] | |
| − | + | * [[Manual:DIL Manual/in|in]] | |
| − | + | * [[Manual:DIL Manual/stringlist|stringlist]] | |
| − | + | * [[Manual:DIL Manual/names|names]] | |
| − | |||
| − | |||
| − | |||
| − | |||
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 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".