Manual:DIL Manual/intlist

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:27, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 position i, counting from 0. It can be assigned to: il.[2] := 50;.
  • length(il) is the number of entries.
  • insert(il, i, n) puts n in at position i, moving the rest up; insert(il, length(il), n) adds it at the end. remove(il, i) takes position i out.

Notes

  • The list doesn't grow by itself: reading il.[i] outside it fails, and assigning outside it does nothing. Use insert() 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's dilcopy); il := null; empties a list.
  • There is no in for 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 dilcopy in a zone file, but not of the dilcopy() 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.

See also