Difference between revisions of "Manual:DIL Manual/length()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = length() = | |
| − | + | '''Function''' (integer). The length of a string, or the number of entries in a list. | |
| − | |||
| − | |||
| − | |||
| − | + | length(s : string) | |
| − | + | length(sl : stringlist) | |
| − | + | length(il : intlist) | |
| − | |||
| − | |||
| − | |||
| − | + | For a string, the number of bytes in it. For a stringlist or an intlist, the number of entries. The usual loop over a list runs <code>i</code> from 0 to <code>length(list) - 1</code>. | |
| + | |||
| + | == Parameters == | ||
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | s, sl, il || string, stringlist or intlist || What to measure. vmc refuses other types. | ||
| + | |} | ||
| + | |||
| + | == Returns == | ||
| + | Integer: the length, 0 for "" or an empty list (and for a string variable that has never had a value). It fails if the argument fails, for example a field of a null unit: <code>length(u.name) > 0</code> and <code>length(u.name) == 0</code> are then both false. | ||
| + | |||
| + | == Notes == | ||
| + | * It counts bytes, not letters on the screen: colour and HTML codes such as <code><br/></code> count, and a letter like "æ" is two bytes. | ||
| + | * <code>length</code> is a reserved word, so it can't be a variable name. | ||
| + | * Read the length once into a variable before a loop if the list doesn't change; it saves the call on every round. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin integer too_long(pc : unitptr, msg : string); | ||
| + | code | ||
| + | { | ||
| + | if (length(msg) > 80) | ||
| + | { | ||
| + | sendtext("Keep it under 80 characters.<br/>", pc); | ||
| + | return (TRUE); | ||
| + | } | ||
| + | return (FALSE); | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Refuses a message longer than 80 characters. For a list, see the example in [[Manual:DIL Manual/split()|split()]]. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/split()|split()]] | ||
| + | * [[Manual:DIL Manual/getwords()|getwords()]] | ||
| + | * [[Manual:DIL Manual/left()|left()]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
| + | * [[Manual:DIL Manual/intlist|intlist]] | ||
Latest revision as of 23:15, 14 November 2026
length()
Function (integer). The length of a string, or the number of entries in a list.
length(s : string) length(sl : stringlist) length(il : intlist)
For a string, the number of bytes in it. For a stringlist or an intlist, the number of entries. The usual loop over a list runs i from 0 to length(list) - 1.
Parameters
| Name | Type | Meaning |
|---|---|---|
| s, sl, il | string, stringlist or intlist | What to measure. vmc refuses other types. |
Returns
Integer: the length, 0 for "" or an empty list (and for a string variable that has never had a value). It fails if the argument fails, for example a field of a null unit: length(u.name) > 0 and length(u.name) == 0 are then both false.
Notes
- It counts bytes, not letters on the screen: colour and HTML codes such as
<br/>count, and a letter like "æ" is two bytes. lengthis a reserved word, so it can't be a variable name.- Read the length once into a variable before a loop if the list doesn't change; it saves the call on every round.
Example
dilbegin integer too_long(pc : unitptr, msg : string);
code
{
if (length(msg) > 80)
{
sendtext("Keep it under 80 characters.<br/>", pc);
return (TRUE);
}
return (FALSE);
}
dilend
Refuses a message longer than 80 characters. For a list, see the example in split().