Difference between revisions of "Manual:DIL Manual/addstring()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = addstring() = | |
| − | + | '''Procedure.''' Adds a string to the end of a stringlist. | |
| − | |||
| − | |||
| − | |||
| − | + | addstring(sl : stringlist, s : string) | |
| − | |||
| − | addstring ( | ||
| − | |||
| − | |||
| − | |||
| − | + | Adds <code>s</code> as the new last element of <code>sl</code>. <code>sl</code> is a stringlist variable or a stringlist field, such as a unit's <code>names</code> or an extra description's <code>names</code>. | |
| + | |||
| + | == Parameters == | ||
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | sl || stringlist || The list: a variable or a field. | ||
| + | |- | ||
| + | | s || string || The string to add. | ||
| + | |} | ||
| + | |||
| + | == Notes == | ||
| + | * Spaces at the start and end are removed, and a row of spaces inside becomes a single space. A string that starts with a space is also logged ("DIL ... addstring [...] has whitespace"). | ||
| + | * It adds the string even when the list has it already. Test with <code>in</code> first to keep a list without repeats. | ||
| + | * When <code>s</code> fails (a field of a null unit), nothing is added. | ||
| + | * <code>sl := null;</code> empties a list (quests.zon does this before building a new one). <code>sl := {};</code> doesn't compile. | ||
| + | * A list constant <code>{...}</code> takes only constants; a list with values from variables is built with <code>addstring</code>. | ||
| + | * <code>insert()</code> adds at a given place; <code>substring()</code> and <code>remove()</code> take elements out. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin who_is_here(); | ||
| + | var | ||
| + | u : unitptr; | ||
| + | sl : stringlist; | ||
| + | i : integer; | ||
| + | code | ||
| + | { | ||
| + | foreach (UNIT_ST_PC, u) | ||
| + | addstring(sl, u.name); | ||
| + | |||
| + | i := 0; | ||
| + | while (i < length(sl)) | ||
| + | { | ||
| + | sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", self); | ||
| + | i := i + 1; | ||
| + | } | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Builds a list of the players in the room and shows it, numbered. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/substring()|substring()]] | ||
| + | * [[Manual:DIL Manual/insert()|insert()]] | ||
| + | * [[Manual:DIL Manual/remove()|remove()]] | ||
| + | * [[Manual:DIL Manual/in|in]] | ||
| + | * [[Manual:DIL Manual/length()|length()]] | ||
| + | * [[Manual:DIL Manual/split()|split()]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
| + | * [[Manual:DIL Manual/names|names]] | ||
Latest revision as of 23:23, 14 November 2026
addstring()
Procedure. Adds a string to the end of a stringlist.
addstring(sl : stringlist, s : string)
Adds s as the new last element of sl. sl is a stringlist variable or a stringlist field, such as a unit's names or an extra description's names.
Parameters
| Name | Type | Meaning |
|---|---|---|
| sl | stringlist | The list: a variable or a field. |
| s | string | The string to add. |
Notes
- Spaces at the start and end are removed, and a row of spaces inside becomes a single space. A string that starts with a space is also logged ("DIL ... addstring [...] has whitespace").
- It adds the string even when the list has it already. Test with
infirst to keep a list without repeats. - When
sfails (a field of a null unit), nothing is added. sl := null;empties a list (quests.zon does this before building a new one).sl := {};doesn't compile.- A list constant
{...}takes only constants; a list with values from variables is built withaddstring. insert()adds at a given place;substring()andremove()take elements out.
Example
dilbegin who_is_here();
var
u : unitptr;
sl : stringlist;
i : integer;
code
{
foreach (UNIT_ST_PC, u)
addstring(sl, u.name);
i := 0;
while (i < length(sl))
{
sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", self);
i := i + 1;
}
quit;
}
dilend
Builds a list of the players in the room and shows it, numbered.