Arrays are used in situations where you want multiple values for a variable. In ABL, there are only one-dimensional arrays. Unlike a list, which only applies to CHARACTER data, an array can be defined for any ABL data type. To define an array, you use the VAR statement or the DEFINE VARIABLE statement. You can optionally specify the size (extent) of the array, that is the number of elements the variable can hold. If you specify a size, then the variable is a determinate array. If no size is specified when defined, then the variable is an indeterminate array and its size can be set at runtime.

Define an array variable

The syntax for defining an array variable using the VAR statement is:
VAR type-name[size] name [ = [val1,...valn] ].
The syntax for defining a variable as an array using the DEFINE VARIABLE statement is:
DEFINE VARIABLE name AS type-name EXTENT [size] [NO-UNDO] [INITIAL [val1,...valn]].
name
The name of the variable.
type-name
An ABL data type.
size
An INTEGER value containing the maximum number of elements the array can hold. If size is specified, then the array is considered a determinate array and its size cannot change at run time. If size is not specified, then the array is considered an indeterminate array and its size can change at run time.
val1,...valn
An optional, comma-separated list of initial values for the variable. The values are enclosed in brackets. You are not required to initialize all elements of the array.

Example

The following statement defines an array to hold the monthly sales quotas for a sales representative. The array is of type INTEGER and contains 12 elements:
VAR INTEGER[12] monthlySalesQuotas.

Access an element of an array

In ABL, array elements are indexed. The first element of the array has an index value of 1. Unlike lists, you cannot efficiently look up an element in an array by value. To find a specific value, you must iterate all elements using an index. To access an element of an array, you enclose the index number in brackets.

VAR INT[12] monthlySalesQuotas = [100500,125000,125000,175000,150000,
  155000,145000,160000,130000,140000,145000,150000].

MESSAGE "Sales quota for April: 
quot; monthlySalesQuotas[4] VIEW-AS ALERT-BOX.
The example code produces the output:
Sales quota for April: $ 175000

For more information, see [ ] Array reference in the ABL Reference.

Determinate and indeterminate arrays

Whether the array is determinate (the size cannot change) or indeterminate (the size can change) depends on its definition.

To define a determinate array variable, specify an integer value for size using either the VAR or DEFINE VARIABLE statement.

To define an indeterminate array variable, do not specify a size in the variable definition statement. If initial values are provided in the definition, then the initial size of the array will be the number of values provided. If no initial values are provided, then the array remains uninitialized and its size must be set at runtime before you can work with it. The size of the indeterminate array can be set and reset at runtime using the EXTENT statement.

The example code shows variable definitions for determinate and indeterminate arrays:
VAR INT[3] a1.           // determinate array
VAR INT[3] a2 = [1,2,3]. // determinate array
VAR INT[] a3.            // indeterminate array
VAR INT[] a4 = [4,5,6].  // indeterminate array

An indeterminate array can be in one of two states: with a size (fixed) or without a size (unfixed). An indeterminate array does not have a size when first defined, unless initial values are provided. You can set the size of an indeterminate array with no size by:

  • providing initial values. The size becomes the number of elements provided.
  • setting the number of elements in the array using the EXTENT statement.
  • assigning an array with a size to the indeterminate array. The size becomes the size of the source array.
  • passing array parameters to a procedure, user-defined function, or class-based method, so that the indeterminate array value is the target for the passing of a source array with a size. The array size becomes the size of the source array.

EXTENT function

To determine how many elements are in an array, you can use the EXTENT function. A numeric value is returned if there are elements. If there are no elements, then the Unknown value (?) is returned.
/* Indeterminate array */
VAR INT[] monthlySalesQuotas = [100500,125000,125000,175000,150000,155000,145000,
  160000,130000,140000,145000,150000].

/* Determinate array */
VAR INT[12] monthlySales.

/* Indeterminate array */
VAR INT[] qtr1Sales.

MESSAGE "monthlySalesQuotas array has" EXTENT(monthlySalesQuotas) "elements." SKIP
  "monthlySales array has" EXTENT(monthlySales) "elements." SKIP
  "qtr1Sales array has" EXTENT(qtr1Sales) "elements."
  VIEW-AS ALERT-BOX.

Running the code produces the output:

monthlySalesQuotas array has 12 elements. 
monthlySales array has 12 elements. 
qtr1Sales array has ? elements.

EXTENT statement

The EXTENT statement sets or resets the size of an indeterminate array after it is defined. The EXTENT statement:
  • Sets the number of elements for an indeterminate array variable or parameter
  • Resizes an indeterminate array variable or parameter
After the size is set, you can use the indeterminate array as you would any determinate array. You cannot use the EXTENT statement on a determinate array; if you do, you get an error.

The example code uses the EXTENT statement to set the number of elements of an indeterminate array.

/* Define an indeterminate array */
VAR INT[] qtr1Sales.

MESSAGE "qtr1Sales array has" EXTENT(qtr1Sales) "elements."
  VIEW-AS ALERT-BOX.

/* Set the number of elements of the indeterminate array to 4 */
EXTENT (qtr1Sales) = 4.  
MESSAGE "Set the size: qtr1Sales array has" EXTENT(qtr1Sales) "elements."
  VIEW-AS ALERT-BOX.

/* Resize the indeterminate array to 12 elements */
EXTENT (qtr1Sales) = 12.  
MESSAGE "Resize the array: qtr1Sales array has" EXTENT(qtr1Sales) "elements."
  VIEW-AS ALERT-BOX.
Running the code produces the output:
qtr1Sales array has ? elements.
Set the size: qtr1Sales array has 4 elements.
Resize the array: qtr1Sales array has 12 elements.

Assign a scalar value to all elements of an array

You can assign a scalar value to all the elements of a determinate array (or indeterminate array with a size) using the assignment operator.
VAR INT[2] a1. // determinate array
VAR INT[] a2. // indeterminate array

a1 = 1. // Assign 1 to each element.
DISPLAY a1.  // Display whole array (size known at compile time)

EXTENT(a2) = 2.  // Set size to 2
a2 = 2.  // Assign 2 to each element
DISPLAY a2[1] a2[2].  // Display elements individually 
                      // (size not known at compile time)
The example code produces the output:
     a1[1]      a1[2]      a2[1]      a2[2]
---------- ---------- ---------- ----------
         1          1          2          2

Assign an array to another array (deep copy)

You can assign one array to another (Deep copy arrays), provided it makes semantic sense to do so. When deep copying one array to another, the following rules apply:

  • You can assign any array to an indeterminate array whose size is not set.
  • You cannot assign an indeterminate array with no size to an array with a size (determinate or indeterminate)
  • If either of the arrays are a determinate array, or an indeterminate array with a size, then the size must match. If the size does not match, then the AVM raises an error.
  • You cannot assign a scalar value to an indeterminate array with no size.

The example code shows a determinate array assigned to another determinate array. They both have the same size.

VAR INT[3] sourceArray = [1,2,3].
VAR INT[3] targetArray.

targetArray = sourceArray.

DISPLAY targetArray.

Running the example code produces the output:

targetArray[1] targetArray[2] targetArray[3]
-------------- -------------- --------------
             1              2              3