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