Manual:DIL Manual/length()
Jump to navigation
Jump to search
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().