Contents | Index | < Browse | Browse >
2.3 Arrays

  Arrays of variables are declared by using the "Dim" instruction and
  stating the number of elements in round brackets:
  +-----------------------------------------------------------------------------
  | 
  | Dim [KEEP] name.t(n)
  |
  +-----------------------------------------------------------------------------

  name is an array of n elements of type .t, starting from index 0
  to #elements-1.
  Same as single variables, all array elements are initialized to zero.
  The keyword "KEEP" is optional and used if the array already exists and the
  content should be kept (only new elements are initialized to zero).
  If the array is down-sized however, data might get lost.
  
  An element is indexed by:
  +-----------------------------------------------------------------------------
  | 
  | name.t(index)
  | 
  +-----------------------------------------------------------------------------
  Note: Negative indices are not allowed. The index must be smaller
        than #elements.

  Any array can be "re-dimmed" any time by calling "Dim KEEP name.t (n)".

  Example:
  +-----------------------------------------------------------------------------
  | 
  | Dim Race.s(5)        ; declare an array of strings called "Race"
  |
  | Race(0) = "Human"    ; fill the elements of the array
  | Race(1) = "Elve"
  | Race(2) = "Dwarf"
  | Race(3) = "Ork"
  | Race(4) = "Halfling"
  |
  | For n.l=0 To 4 
  |   NPrint Race(n)     ; print out all 5 race names
  | Next
  | 
  +-----------------------------------------------------------------------------
  Note: The last element is 4, since we have 5 elements, from 0..4.
        Using 5 or higher as index would cause a runtime error!

  Arrays (with some restrictions) can be also part of a newtype. See 2.6 Newtypes
  for details.
	
2.3.1 Multi-Dimensional Arrays

  Arrays can be multi-dimensional. The dimensions are seperated by "," (comma).

  Example:
  +-----------------------------------------------------------------------------
  | 
  | Dim Matrix.l(10,10)  ; define a 10x10 matrix of long intergers
  | 
  | Matrix(0,0) = 1
  | Matrix(4,2) = 4
  |
  | For x.l=0 To 9 
  |   For y.l=0 To 9 
  |     NPrint Matrix(x,y)  ; print out all elements of the matrix
  |   Next
  | Next
  | 
  +-----------------------------------------------------------------------------
  Note: Be careful with multi-dimensional arrays. They can quickly eat up
        a lot of memory. E.g. Dim x.l(100,100,100) would alread eat up 4MB!
  

Contents | Index | < Browse | Browse >