Difference between revisions of "Manual:DIL Manual/insert()"
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: | ||
| + | = insert() = | ||
| + | '''Procedure.''' Puts a new entry into a stringlist or an intlist at a given position. | ||
| + | insert(list : stringlist or intlist, i : integer, value : string or integer) | ||
| − | < | + | The entries from position <code>i</code> on move up one place. Positions count from 0, so |
| + | <code>insert(sl, 0, s)</code> puts <code>s</code> first. It works on a list variable and on the | ||
| + | list fields of an extra (<code>ex.names</code>, <code>ex.vals</code>, | ||
| + | <code>u.extra.["name"].names</code>), which is how comm.zon keeps a player's tell history | ||
| + | newest first. | ||
| − | + | == Parameters == | |
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | list || stringlist or intlist || The list to change: a variable or a list field, not a function's result. | ||
| + | |- | ||
| + | | i || integer || The position the new entry gets. | ||
| + | |- | ||
| + | | value || string or integer || The new entry. An integer put into a stringlist becomes its digits; a string put into an intlist becomes its number, as with <code>atoi()</code>. | ||
| + | |} | ||
| − | + | == Notes == | |
| − | + | * A stringlist and an intlist treat a position past the end differently. In a stringlist, | |
| − | + | empty strings fill the gap: <code>insert(sl, 5, "x")</code> on an empty list gives six | |
| − | + | entries, the last <code>"x"</code>. In an intlist, a position past the end, or below 0, adds | |
| − | + | nothing; <code>i</code> equal to the length adds at the end. | |
| − | + | * '''Never give a stringlist a negative position.''' It stops the server. A huge position makes | |
| + | it try to build a list that long. | ||
| + | * When <code>i</code> is a failed value (a field of null), the entry goes in at position 0. | ||
| + | * An unset string inserts nothing. | ||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | dilbegin add_history(ch : unitptr, s : string); | |
| − | + | var | |
| − | + | ex : extraptr; | |
| − | + | code | |
| − | + | { | |
| − | + | ex := "$tell_history" in ch.extra; | |
| − | + | if (ex == null) | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
{ | { | ||
| − | + | addextra(ch.extra, {"$tell_history"}, "history"); | |
| − | + | ex := "$tell_history" in ch.extra; | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | insert(ex.names, 1, s); | ||
| + | while (length(ex.names) > 21) | ||
| + | remove(ex.names, length(ex.names) - 1); | ||
| + | return; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Keeps the 20 latest tells behind the extra's name, newest first, as comm.zon does. | ||
| − | + | == See also == | |
| + | * [[Manual:DIL Manual/remove()|remove()]] | ||
| + | * [[Manual:DIL Manual/addstring()|addstring()]] | ||
| + | * [[Manual:DIL Manual/substring()|substring()]] | ||
| + | * [[Manual:DIL Manual/length()|length()]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
| + | * [[Manual:DIL Manual/intlist|intlist]] | ||
Latest revision as of 23:20, 14 November 2026
insert()
Procedure. Puts a new entry into a stringlist or an intlist at a given position.
insert(list : stringlist or intlist, i : integer, value : string or integer)
The entries from position i on move up one place. Positions count from 0, so
insert(sl, 0, s) puts s first. It works on a list variable and on the
list fields of an extra (ex.names, ex.vals,
u.extra.["name"].names), which is how comm.zon keeps a player's tell history
newest first.
Parameters
| Name | Type | Meaning |
|---|---|---|
| list | stringlist or intlist | The list to change: a variable or a list field, not a function's result. |
| i | integer | The position the new entry gets. |
| value | string or integer | The new entry. An integer put into a stringlist becomes its digits; a string put into an intlist becomes its number, as with atoi().
|
Notes
- A stringlist and an intlist treat a position past the end differently. In a stringlist,
empty strings fill the gap:insert(sl, 5, "x")on an empty list gives six entries, the last"x". In an intlist, a position past the end, or below 0, adds nothing;iequal to the length adds at the end.
- Never give a stringlist a negative position. It stops the server. A huge position makes
it try to build a list that long.
- When
iis a failed value (a field of null), the entry goes in at position 0. - An unset string inserts nothing.
Example
dilbegin add_history(ch : unitptr, s : string);
var
ex : extraptr;
code
{
ex := "$tell_history" in ch.extra;
if (ex == null)
{
addextra(ch.extra, {"$tell_history"}, "history");
ex := "$tell_history" in ch.extra;
}
insert(ex.names, 1, s);
while (length(ex.names) > 21)
remove(ex.names, length(ex.names) - 1);
return;
}
dilend
Keeps the 20 latest tells behind the extra's name, newest first, as comm.zon does.