Difference between revisions of "Manual:DIL Manual/integerlist"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = integerlist = | |
| − | + | '''Type.''' The keyword is <code>intlist</code>. | |
| − | + | il : intlist; | |
| − | + | A list of integers, numbered from 0. Declare it as <code>intlist</code> (there is no keyword <code>integerlist</code>). The <code>vals</code> field of an extra description is also an intlist, and so is what <code>weapon_info()</code> gives. | |
| − | + | == Using it == | |
| − | ; | + | {| class="wikitable" |
| + | ! Write !! What it does | ||
| + | |- | ||
| + | | <code>il := {10, 20, 30};</code> || sets the whole list; the values must be constants or macros, not variables | ||
| + | |- | ||
| + | | <code>il.[1]</code> || the second value; read and write | ||
| + | |- | ||
| + | | <code>length(il)</code> || how many values it has | ||
| + | |- | ||
| + | | <code>insert(il, i, v);</code> || puts <code>v</code> in at position <code>i</code>, moving the rest up; <code>i</code> from 0 to <code>length(il)</code> | ||
| + | |- | ||
| + | | <code>remove(il, i);</code> || takes out the value at position <code>i</code> | ||
| + | |- | ||
| + | | <code>il := null;</code> || empties it | ||
| + | |- | ||
| + | | <code>il := other;</code> || copies another intlist | ||
| + | |} | ||
| − | + | == Notes == | |
| + | * An index outside the list fails: reading <code>il.[5]</code> of a list of three is a failed value (false in every comparison), and assigning to it does nothing. The list doesn't grow by assignment; add values with <code>insert(il, length(il), v)</code>. | ||
| + | * <code>insert()</code> with a position past the end, and <code>remove()</code> with one outside the list, do nothing. | ||
| + | * A new intlist variable is empty. Unlike unit and extra variables, an intlist keeps its values across <code>wait()</code> and <code>pause</code>. | ||
| + | * <code>in</code> doesn't work on intlists; walk the list with a <code>while</code> loop. | ||
| + | * <code>{...}</code> can't hold variables: <code>{n, 2}</code> is a syntax error. Build such a list with <code>insert()</code>. | ||
| + | * An intlist can be a template's argument; from a zone file it's written <code>{1, 2, 3}</code>. <code>dilcopy()</code> from DIL can't pass lists. | ||
| − | + | == Example == | |
| + | <pre> | ||
| + | dilbegin integer sumlist(il : intlist); | ||
| + | var | ||
| + | i : integer; | ||
| + | sum : integer; | ||
| + | code | ||
| + | { | ||
| + | i := 0; | ||
| + | sum := 0; | ||
| + | while (i < length(il)) | ||
| + | { | ||
| + | sum := sum + il.[i]; | ||
| + | i := i + 1; | ||
| + | } | ||
| + | return (sum); | ||
| + | } | ||
| + | dilend | ||
| − | + | dilbegin listdemo(); | |
| − | + | external | |
| + | integer sumlist@ex(il : intlist); | ||
| + | var | ||
| + | il : intlist; | ||
| + | n : integer; | ||
| + | code | ||
| + | { | ||
| + | il := {10, 20, 30}; | ||
| + | il.[1] := 25; | ||
| + | insert(il, length(il), 40); | ||
| + | remove(il, 0); | ||
| + | n := sumlist@ex(il); | ||
| + | log("Sum is " + itoa(n)); | ||
| + | il := null; | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Builds the list 25, 30, 40 and logs "Sum is 95". | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/intlist|intlist]] | |
| − | + | * [[Manual:DIL Manual/insert()|insert()]] | |
| − | + | * [[Manual:DIL Manual/remove()|remove()]] | |
| − | + | * [[Manual:DIL Manual/length()|length()]] | |
| − | + | * [[Manual:DIL Manual/vals|vals]] | |
| − | + | * [[Manual:DIL Manual/stringlist|stringlist]] | |
| − | + | * [[Manual:DIL Manual/Data Types|Data Types]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 23:25, 14 November 2026
integerlist
Type. The keyword is intlist.
il : intlist;
A list of integers, numbered from 0. Declare it as intlist (there is no keyword integerlist). The vals field of an extra description is also an intlist, and so is what weapon_info() gives.
Using it
| Write | What it does |
|---|---|
il := {10, 20, 30}; |
sets the whole list; the values must be constants or macros, not variables |
il.[1] |
the second value; read and write |
length(il) |
how many values it has |
insert(il, i, v); |
puts v in at position i, moving the rest up; i from 0 to length(il)
|
remove(il, i); |
takes out the value at position i
|
il := null; |
empties it |
il := other; |
copies another intlist |
Notes
- An index outside the list fails: reading
il.[5]of a list of three is a failed value (false in every comparison), and assigning to it does nothing. The list doesn't grow by assignment; add values withinsert(il, length(il), v). insert()with a position past the end, andremove()with one outside the list, do nothing.- A new intlist variable is empty. Unlike unit and extra variables, an intlist keeps its values across
wait()andpause. indoesn't work on intlists; walk the list with awhileloop.{...}can't hold variables:{n, 2}is a syntax error. Build such a list withinsert().- An intlist can be a template's argument; from a zone file it's written
{1, 2, 3}.dilcopy()from DIL can't pass lists.
Example
dilbegin integer sumlist(il : intlist);
var
i : integer;
sum : integer;
code
{
i := 0;
sum := 0;
while (i < length(il))
{
sum := sum + il.[i];
i := i + 1;
}
return (sum);
}
dilend
dilbegin listdemo();
external
integer sumlist@ex(il : intlist);
var
il : intlist;
n : integer;
code
{
il := {10, 20, 30};
il.[1] := 25;
insert(il, length(il), 40);
remove(il, 0);
n := sumlist@ex(il);
log("Sum is " + itoa(n));
il := null;
quit;
}
dilend
Builds the list 25, 30, 40 and logs "Sum is 95".