EGL Reference Guide for iSeries

Dynamic arrays of records and data items

You can declare an array of records or data items without specifying the number of elements, and in this case the number varies at run time as your code invokes system functions that append, insert, or delete elements or that remove the array. This dynamic array is useful for a variety of tasks but is not available in forms.

The syntax for declaring a dynamic array includes brackets with no size value, as in the following examples:

  // an array of 5 items or less
  myDataItem01 CHAR(30)[] { maxSize=5 };
 
  // an array of 6 items or less
  myDataItem02 myDataItemPart[] { maxSize=6 };
 
  // an array of the maximum number of items 
  // (the maximum depends on the generated language)
  myRecord myRecordPart[];

The rules for referencing the elements of a dynamic array are equivalent to the rules for referencing a static array. If your code refers to a dynamic array of records or data items and does not specify brackets or a subscript, the reference is to the array as a whole.

An out-of-memory situation is treated as a catastrophic error and ends the program.

Functions and variables for processing dynamic arrays

A set of functions and read-only variables are available for each dynamic array. In the following descriptions, substitute the array name for arrayName and note that the name may be qualified by a package name, a library name, or both:

arrayName.appendAll(appendArray)
This function does as follows:

The elements of appendArray must be of the same type as the elements of arrayName.

arrayName.appendElement(content)
This function places an element to the end of the specified array and increments the size by one. For content, you can substitute a variable of the appropriate type; alternatively, you can specify a literal that is assigned to an element created during the operation. The process copies data; if you assign a variable, that variable is still available for comparison or other purposes.

The rules for assigning a literal are as specified in Assignments.

arrayName.insertElement(content, index)
This function does as follows:

content is the new content (a constant or variable of the appropriate type for the array), and index is an integer literal or a numeric variable that indicates the location of the new element.

If index is one greater than the number of elements in the array, the function creates a new element at the end of the array and increments the array size by one.

arrayName.removeAll()
This function removes the elements of the array from memory. The array can be used, but its size is zero.
arrayName.removeElement(index)
This function removes the element at the specified location, decrements the array size by one, and decrements the index of each element that resides after the removed element.

index is an integer literal or a numeric variable that indicates the location of the element to be removed.

sysLib.maximumSize
As described in sysLib.maximumSize.
sysLib.size
As described in sysLib.size.

Use of dynamic arrays as arguments and parameters

A dynamic array can be passed as an argument to an EGL function. The related parameter must be defined as a dynamic array of the same type as the argument; and for a data item, the type must include the same length and decimal places, if any.

A dynamic array cannot be passed as an argument to another program.

An example of a function that uses a dynamic array as a parameter is as follows:

  Function getAll (employees Employee[])
   ;
  end

At run time, the maximum size for a parameter is the maximum size declared for the corresponding argument. The function or called program can change the size of the array, and the change is in effect in the invoking code.

SQL processing and dynamic arrays

EGL lets you use a dynamic array to access rows of a relational database. For details on reading multiple rows, see get. For details on adding multiple rows, see add.


Related concepts
Compatibility with VisualAge Generator
References to variables and constants


Related reference
add
Assignments
EGL system limits
get
in
sysLib.size


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]