Tutorial - Arrays of Structures

Given that you can have arrays of any type and you can create your own structure type it will be no surprise that you can have an array of structures.

Here is an example :-

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

Program()
	MyStrArray myarray;
    Begin
	myarray[2].id := 142;
	Output myarray[2];
    End;

This will produce the output :-

{142, "Robin", Null}

Just as you can have a record in an array, you can have an array in a record. As a general rule - if it makes sense you can do it.

Type MySubArray = Number[1 To 4];
Type MyStruct = { Number id = 100, Const Text name = "Robin", MySubArray list };

Program()
	MyStruct myrecord;
    Begin
	myrecord.list[2] := 42;
	Output myrecord;
    End;

This will produce the output :-

{100, "Robin", [ Null, 42, Null, Null ]}

Here is a more complex example. Everything could done in a simple loop without using an array or even a structure ( that is left as an exercise for the reader ) but it illustrates the use of arrays.

array_2.grs Output of array_2.grs array_2.png