Contents | Prev | Next

6.6 List variables

Description

List variables are used to store values of list types (i.e. collections of values of the same type). The type of each value in a list is called the list's component type. An list's component type may be any type, including a list type, so it is possible to define lists of lists.

The built-in procedure new must be used to initialize list variables before they can be used to store values, and the built-in procedure dispose should be used to clean-up after list variables that are no longer required.

The number of values in each list is theorectically unlimited (in practice the amount of memory available will limit the size of lists). Each value in a list is identified by index value of integral type. Each value in a list is stored in a seperate component of the list called a list element (or a selected variable). Each element of a list can be referenced individually, in order to store values into, and retrieve values from specific elements. See the  syntax  section below for details.

Selected Variables

Selected variables are created by calling the built-in procedure insert to insert values into lists, and selected variables are destroyed by calling the built-in procedure delete to remove values from lists. NOTE: The built-in procedure dispose will remove all values from lists.

Selected variables are referenced by indexing the list variable that were used to create them.

Example

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

program sort(output);
const
   max = 20;
var
   numbers : list of integer;

   procedure GenerateRandomNumbers;
   var
      i : 1..max;
   begin
      for i := 1 to max do
         insert(random(100)+1, numbers);
   end;

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

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

begin
   randomize;
   new(numbers);
   GenerateRandomNumbers;
   SortNumbers;
   PrintNumbers;
   dispose(numbers);
end.

Syntax

The syntax for referencing selected variables is given below:

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

   selected-variable = list-variable '[' index-expression { ',' index-expression } ']'

   index-expression = expression

where list-variable is a reference to a list variable and index-expression is an expression, of integral type, that evaluates to the position of a selected variable in the list.

Contents | Prev | Next