Tutorial - Open Array Definitions

Defining an Open Array Type

In the previous examples all the dimensions of the arrays have been fully specified. This need not be the case. It is possible to leave a range blank and it will be set later. It should always be remembered that once an actual array has been created it will have a fixed range for all dimensions ( it is the type definition that is open rather than the array ).

Here is an example of an open array definition.

Type MyStruct = { Number id = 100, Const Text name = "Robin", Text addr };
Type MyStrArray = MyStruct[];

Once an open array type has been defined it can be used to declare an actual array. If the declared array is not initialised the array as an object wil be Null. Compare this to an uninitialised fixed size array where an uninitialised array will exist with all the fields initialised.

Study the two types of array in the following example :-

Type MyStruct = { Number id = -1, Text name = "-unknown-" };
Type MyFixedArray = MyStruct[1 To 4];
Type MyOpenArray = MyStruct[];

Program()
	MyFixedArray fixedarray;
	MyOpenArray openarray;
    Begin
	Output "fixedarray", fixedarray;
	Output "openarray", openarray;
    End;

The program will generate the following output :-

"fixedarray", [ {-1, "-unknown-"}, {-1, "-unknown-"}, {-1, "-unknown-"}, {-1, "-unknown-"} ]
"openarray", Null

Creating an Open Array

There are a number of ways of creating an open array. Copying an existing array, specifying a literal array and calling the Array() function.

Copying an Existing Array

If you already have a compatible array you can just assign the value to the open array variable. This will generate a copy of the original. In the following example we are copying the automatically initialised fixed array. Note: the copy will be a completely separate array - it will not be a reference to the original.

	openarray := fixedarray;

Specifying a Literal Array

You can write an entire array.

	openarray := [ {1, "Robin"}, {2, "Fred"} ];

This will assign the array to the variable. This type of literal array can also be used for the variable initialiser.

The Array() Function

There is a special function to allocate a new array. The type of the array is picked up from the variable to which it is being assigned. The parameters specify the lengths of any open dimensions. The low bound of the index will always be set to 1 and the high bound will be set to the count. All the elements of the array will be initialised.

	openarray := Array(3);