Summary:
See also: Variables, Records, Data Types.
Arrays can store a one-, two- or three-dimensional array of elements.
ARRAY [ intconst [,intconst [,intconst]
] ] OF datatype
DYNAMIC
ARRAY [ WITH DIMENSION rank ] OF datatype
Object Methods | |
Name | Description |
appendElement( ) |
Adds a new element at the end of a dynamic array. This method has no effect on a static array. |
clear( ) |
Removes all elements in a dynamic array. Sets all elements to NULL in a static array. |
deleteElement( INTEGER ) |
Removes an element at the given position. In a static and dynamic array, the elements after the given position are moved up. In a dynamic array, the number of elements is decremented by 1. |
getLength( ) RETURNING INTEGER |
Returns the length of a one-dimensional array. |
insertElement( INTEGER ) |
Inserts a new element at the given position. In a static and dynamic array, the elements after the given position are moved down. In a dynamic array, the number of elements is incremented by 1. |
Arrays can store a one-, two- or three-dimensional array of variables, all of the same type. These can be any of the supported data types or a record definition, but it cannot be another array (ARRAY .. OF ARRAY).
The first syntax (ARRAY[i[,j[,k]]]) defines traditional static arrays, which are defined with an explicit size for all dimensions. Static arrays have a size limit.
The second syntax (DYNAMIC ARRAY) define arrays with a variable size. Dynamic arrays have no theoretical size limit. The elements of dynamic arrays are allocated automatically by the runtime system, according to the indexes used.
01
MAIN02
DEFINE a1 ARRAY[100] OF INTEGER -- This is a static array03
DEFINE a2 DYNAMIC ARRAY OF INTEGER -- This is a dynamic array04
LET a1[50] = 1245605
LET a2[5000] = 12456 -- Automatic allocation for element 500006
LET a1[5000] = 12456 -- Runtime error!07
END MAIN
When using the array, you can specify the index with an integer expression:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
DEFINE i INTEGER04
LET i = 1205
LET a[500+i] = 1245606
END MAIN
For dynamic arrays, memory is allocated dynamically. Large array indexes can be used without consuming a lot of memory resources.
01
SCHEMA stores02
MAIN03
DEFINE a DYNAMIC ARRAY OF RECORD LIKE customer.*04
DEFINE i INTEGER05
DATABASE stores06
DECLARE c CURSOR FOR SELECT * FROM customer07
LET i=008
FOREACH c INTO a[i+1].*09
LET i=i+110
END FOREACH11
CALL a.deleteElement(i+1)12
DISPLAY "Rows found: ", a.getLength()13
END MAIN
a[i+1].*
, an additional element is allocated during the last FETCH
returning NOTFOUND. Therefore, the last element must be removed after
the FOREACH loop. Here is the recommended way to fetch rows into a dynamic array:
01
SCHEMA stores02
MAIN03
DEFINE a DYNAMIC ARRAY OF RECORD LIKE customer.*04
DEFINE r RECORD LIKE customer.*05
DEFINE i INTEGER06
DATABASE stores07
DECLARE c CURSOR FOR SELECT * FROM customer08
LET i=009
FOREACH c INTO r.*10
LET i=i+111
LET a[i].* = r.*12
END FOREACH13
DISPLAY "Rows found: ", a.getLength()14
END MAIN
If you reference an array element in an r-value, with an index outside the allocated dimensions, you get a -1326 runtime error:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[50] = 1245604
DISPLAY a[100] -- Runtime error05
END MAIN
Arrays can be queried with the getLength()
method, to get the number
of allocated elements:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[5000] = 1245604
DISPLAY a.getLength()05
END MAIN
You can insert a new element at a given position with the insertElement()
method. The new element will be initialized to NULL. All subsequent elements are
moved down by an offset of +1. Dynamic arrays will grow by 1, while static
arrays will lose the last element:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[10] = 1104
CALL a.insertElement(10)05
LET a[10] = 1006
DISPLAY a.getLength() -- shows 1107
DISPLAY a[10] -- shows 1008
DISPLAY a[11] -- shows 1109
END MAIN
You can append a new element at
the end of a dynamic array with the appendElement()
method. The new element will be initialized to NULL. Dynamic arrays will grow by 1, while static
arrays will not be affected by this method:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[10] = 1004
CALL a.appendElement()05
LET a[a.getLength()] = a.getLength()06
DISPLAY a.getLength() -- shows 1107
DISPLAY a[10] -- shows 1008
DISPLAY a[11] -- shows 1109
END MAIN
The deleteElement()
method can be used to remove elements from a
static or dynamic array. Subsequent elements are moved up by an offset of -1.
Dynamic arrays will shrink by 1, while static arrays will have NULLs in the last
element.
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[10] = 904
CALL a.deleteElement(5)06
DISPLAY a.getLength() -- shows 907
DISPLAY a[9] -- shows 908
END MAIN
You can clear an array with the clear()
method. When used on a static array, this method sets all elements to NULL. When
used on a dynamic array, it removes all elements:
01
MAIN02
DEFINE a DYNAMIC ARRAY OF INTEGER03
LET a[10] = 1104
DISPLAY a.getLength() -- shows 1005
CALL a.clear()06
DISPLAY a.getLength() -- shows 007
END MAIN
Array methods can be used on two- and three-dimensional arrays with the brackets notation:
01
MAIN02
DEFINE a2 DYNAMIC ARRAY WITH DIMENSION 2 OF INTEGER03
DEFINE a3 DYNAMIC ARRAY WITH DIMENSION 3 OF INTEGER04
LET a2[50,100] = 1245605
LET a2[51,1000] = 1245606
DISPLAY a2.getLength() -- Displays 5107
DISPLAY a2[50].getLength() -- Displays 10008
DISPLAY a2[51].getLength() -- Displays 100009
LET a3[50,100,100] = 1245610
LET a3[51,101,1000] = 1245611
DISPLAY a3.getLength() -- Displays 5112
DISPLAY a3[50].getLength() -- Displays 10013
DISPLAY a3[51].getLength() -- Displays 10114
DISPLAY a3[50,100].getLength() -- Displays 10015
DISPLAY a3[51,101].getLength() -- Displays 100016
CALL a3[50].insertElement(10) -- Inserts at 50,1017
CALL a3[50,10].insertElement(1)-- Inserts at 50,10,118
END MAIN
01
MAIN02
DEFINE a1 DYNAMIC ARRAY OF INTEGER03
DEFINE a2 DYNAMIC ARRAY WITH DIMENSION 2 OF INTEGER04
DEFINE a3 ARRAY[10,20] OF RECORD05
id INTEGER,06
name VARCHAR(100),07
birth DATE08
END RECORD09
LET a1[5000] = 1245610
LET a2[5000,300] = 1245611
LET a3[5,1].id = a1[50]12
LET a3[5,1].name = 'Scott'13
LET a3[5,1].birth = TODAY14
END MAIN