Array variables


Array variables are used to store values of array types (i.e. collections of values of the same type). The type of each value in an array is called the array's component type. An array's component type may be any type, including an array type, so it is possible to define arrays of arrays (also called multi-dimensional arrays).

A reference to an array variable is a reference to the entire array as a unit, and can be used to both store array values into, and retreive array values from the array variable.

The number of values in each array is fixed, and each value in the array is identified by a value of the array's index type (which is specified when the array is defined). Each value in an array is stored in a seperate component of the array called an array element (or an indexed variable). Each element of an array can be referenced individually, in order to store values into, and retrieve values from specific elements. See the  syntax  section below for details.


Below are some examples of array variable declarations.

   scores : array[1..200] of integer;
   name : array[IndexType] of real;
   students : array[1..MAX_STUDENTS] of student;
   grades : array[1..10] of array[1..5] of integer;
   grades : array[1..10, 1..5] of integer;

NOTE: These examples assume that IndexType is a previously defined ordinal type, MAX_STUDENTS is a previously defined constant of integral type, and student is a previously defined type. Also the two declarations shown for the array grades are equivalent.

Here is a simple example program that generates some random numbers, stores them in an array, sorts the numbers, and then prints the numbers out.

program sort(output);
   max = 20;
   numbers : array[1..max] of integer;

   procedure GenerateRandomNumbers;
      i : 1..max;
      for i := 1 to max do
         numbers[i] := random(100)+1;

   procedure SortNumbers;
      i : 1..max;
      swapped : boolean;
      temp : integer;
         swapped := false;
         for i := 1 to max-1 do
            if numbers[i] > numbers[i+1] then
                  temp := numbers[i];
                  numbers[i] := numbers[i+1];
                  numbers[i+1] := temp;
                  swapped := true;
      until not swapped;

   procedure PrintNumbers;
      i : 1..max;
      for i := 1 to max do
         writeln(i, numbers[i]);



See array types for the syntax for defining new array types.

The syntax for referencing array elements (i.e. indexed variables) is given below:

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   indexed-variable = indexed-variable-array | indexed-variable-string

   array-variable = variable-access

   index-expression = expression

   indexed-variable-array = array-variable '[' index-expression { ',' index-expression } ']'

Where array-variable is a reference to an array variable, and index-expression is an expression which evaluates to one of the values specified by the array's index type.