5.02 Arrays


Within LynPlex an array only has a single dimension, but this does not prevent yuo having multi-dimension arrays. Within an array each element can be any data type including an array. It is therefore possible to define an element of an array as another array. This process can be repeated as many times as required to create multi-dimensional arrays.

Arrays can be created as the output of various commands and functions. They can also be created by direct assignment.

The following statement creates a simple array of four elements.

a = {1, 2, 3, 4}

Nested arrays are allowed and can be assigned to array elements.

a = {{1,2} , {3,4}}
b = {1, 2, 3}
c = {4, 5}
b(2) = c
put b            // displays {1,2,{4,5},3}

Elements of an array can be accessed either using subscripts in parentheses or by using hierarchical notation or even a mixture of the two.

Subscript Notation

To use subscript notation the elements of any array are enclosed in parentheses as:

a = {{1, 2}, {3, 4}}
b = {"a", "b", "c"}
a(3) = b
put a a(2,3,2) a(3,3)

Hierarchical Notation

With hierarchical notation elements of an array are referenced as if they were part of a structure.

The above example could also be written as:

a = {{1, 2}, {3, 4}}
b = {"a", "b", "c"}
a.3 = b
put a a.2.3.2 a.3.3

Array Dimensions

By default the lower bound of an array is always 1.

The DIM command can be used to assign arrays and to provide a range of values for the subscripts including an alternative lower bound. The created array will be empty.

dim m(1 to 3, -1 to 1)
put " m =" m     //display m = {{,,},{,,},{,,}}

Use the DIM command to create arrays to be used as matrices. See 5.04 Array Commands.

Array Default Values

Each array can be supplied with a default value. This is used for any element of an array that is not initialised. This includes elements that have not been defined.

To assign a default the array is entered in an assignment statement without a subscript.

a() = 'red'
put a(2)      // red
b.  = 'green'  
put a.5 b.5   // red green

A hierarchy of arrays can have a default value assigned at any level.

lynplex/lp0502.txt · Last modified: 2011/09/08 16:07 (external edit)