Manual:DIL Manual/intlist
Jump to navigation
Jump to search
intlist
Type. An ordered list of integers.
var il : intlist;
An intlist keeps a row of numbers in one variable: a table to look values up in, or the numbers of an extra description (its vals). A list is written as integer constants in braces: {10, -3, 0x10, MAT_IRON}.
An intlist variable holds its own list. Assigning a list to it copies the numbers, and a template that gets an intlist argument gets a copy as well.
Working with an intlist
il.[i]is the number at positioni, counting from 0. It can be assigned to:il.[2] := 50;.length(il)is the number of entries.insert(il, i, n)putsnin at positioni, moving the rest up;insert(il, length(il), n)adds it at the end.remove(il, i)takes positioniout.
Notes
- The list doesn't grow by itself: reading
il.[i]outside it fails, and assigning outside it does nothing. Useinsert()to make it longer. (Older pages said that the gap is filled with zeros; it isn't.) {...}may hold only constants: numbers, macros, and negative numbers.il := {};doesn't compile (an empty{}only works as an argument in a zone file'sdilcopy);il := null;empties a list.- There is no
infor intlists: walk the list to look for a number. - Intlist variables keep their value through waits and are saved with the unit.
- An intlist can be the argument of a template copied with
dilcopyin a zone file, but not of thedilcopy()procedure. - The wiki also had this type under the name integerlist; the keyword is
intlist.
Example
dilbegin integer roll_bonus();
var
table : intlist;
code
{
// index 2..16 is the sum of 2d8; 0 and 1 are never used
table := {0, 0, -3, -2, -2, -1, -1, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3};
return (table.[rnd(1, 8) + rnd(1, 8)]);
}
dilend
dilbegin integer sum_list(il : intlist);
var
i, sum : integer;
code
{
while (i < length(il))
{
sum := sum + il.[i];
i := i + 1;
}
return (sum);
}
dilend
dilbegin grow_list();
external
integer sum_list(il : intlist);
var
il : intlist;
n : integer;
code
{
il := {5, 10};
insert(il, length(il), 20); // append: il is now {5, 10, 20}
il.[0] := 1; // {1, 10, 20}
n := sum_list(il);
log("sum is " + itoa(n)); // sum is 31
quit;
}
dilend
A lookup table for a 2d8 roll (as the roll8 table in randomt.zon), a function that adds up a list, and a program that grows one.