Difference between revisions of "Manual:DIL Manual/integerlist"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
Line 1: Line 1:
−
<span id="intlist"></span>
+
= integerlist =
−
'''Intlist:'''
+
'''Type.''' The keyword is <code>intlist</code>.
  
−
  Intlists are an array of integer types. They can be set directly as follows:
+
  il : intlist;
  
−
'''Example'''
+
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.
  
−
wpn:=
+
== 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
 +
|}
  
−
The intlists can be accessed in the same way stringlist are accessed as follows.
+
== 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'''
+
== 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
  
−
if (wpn.[5]==5)
+
dilbegin listdemo();
−
The intlists are saved on savable units.
+
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".
  
−
'''Type:  '''<i><!--CODE-->intlist</i><!--ENDCODE-->
+
== See also ==
−
 
+
* [[Manual:DIL Manual/intlist|intlist]]
−
  This variable type allows you to keep an ordered list of integers with out
+
* [[Manual:DIL Manual/insert()|insert()]]
−
having to create a variable for each integer.
+
* [[Manual:DIL Manual/remove()|remove()]]
−
'''Example:'''  <i><!--CODE-->Setting an intlist</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/length()|length()]]
−
<i><!--CODE-->
+
* [[Manual:DIL Manual/vals|vals]]
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/stringlist|stringlist]]
−
 
+
* [[Manual:DIL Manual/Data Types|Data Types]]
−
myintlist:=
 
−
;
 
−
myintlist.[10]:=50;
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
 
 
−
  If you set a index that is higher then the highest index, every index in
 
−
between will be set to zero.  For example if you only have two values in the
 
−
intlist and you set index value 10 to 50 all indexes between 2 and 10 will be
 
−
set to 0.
 
−
'''Example:'''  <i><!--CODE-->Using intlists</i><!--ENDCODE-->
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
if (myintlist.[5]==5)
 
−
if (myintlist&lt;myint)
 
−
i:=0;
 
−
ln:=length(myintlist);
 
−
while (i&lt;ln)
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
'''See Also:'''
 
−
[[#bpinsert|Insert]]
 
−
[[#bpremove|Remove]]
 
−
[[#eptr|Extraptr]]
 
−
<span id="intlist"></span>
 
−
'''Intlist:'''
 
−
 
 
−
  Intlists are an array of integer types.  They can be set directly as follows:
 
−
 
 
−
'''Example'''
 
−
 
 
−
wpn:=
 
−
;
 
−
 
 
−
The intlists can be accessed in the same way stringlist are accessed as follows.
 
−
 
 
−
'''Example'''
 
−
 
 
−
if (wpn.[5]==5)
 
−
The intlists are saved on savable units.
 
−
 
 
−
'''Type:  '''<i><!--CODE-->intlist</i><!--ENDCODE-->
 
−
 
 
−
  This variable type allows you to keep an ordered list of integers with out
 
−
having to create a variable for each integer.
 
−
'''Example:'''  <i><!--CODE-->Setting an intlist</i><!--ENDCODE-->
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
myintlist:=
 
−
;
 
−
myintlist.[10]:=50;
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
 
 
−
  If you set a index that is higher then the highest index, every index in
 
−
between will be set to zero.  For example if you only have two values in the
 
−
intlist and you set index value 10 to 50 all indexes between 2 and 10 will be
 
−
set to 0.
 
−
'''Example:'''  <i><!--CODE-->Using intlists</i><!--ENDCODE-->
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
if (myintlist.[5]==5)
 
−
if (myintlist&lt;myint)
 
−
i:=0;
 
−
ln:=length(myintlist);
 
−
while (i&lt;ln)
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
'''See Also:'''
 
−
[[#bpinsert|Insert]]
 
−
[[#bpremove|Remove]]
 
−
[[#eptr|Extraptr]]
 

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 with insert(il, length(il), v).
  • insert() with a position past the end, and remove() 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() and pause.
  • in doesn't work on intlists; walk the list with a while loop.
  • {...} can't hold variables: {n, 2} is a syntax error. Build such a list with insert().
  • 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".

See also