This module covers detailed explanation and the usage of different types of arrays and data structures.
Arrays:
In RPGLE, an array is a collection of elements having the same data type and length. Array elements are stored in contiguous memory location. Each element can be accessed through a subscript, which starts from 1. The dimension of the array represents the number of elements.
Types of Arrays:
From the perspective of when the array gets loaded, there are three types of arrays:
Compile-time array: Array is loaded at program creation time itself as the data for the array is supplied in the program source itself. CTDATA keyword is mandatory. PERRCD keyword denotes the number of elements for which the data is coded in one record/line. The data for the array should be coded at the bottom of the source with ** in the first two positions
Example:
DMNTH_ARR S 3A DIM(12) PERRCD(6) CTDATA //Fixed format
DCL-S MONTH_ARR CHAR(3) DIM(12) PERRCD(6) CTDATA; //Free format
**CTDATA MNTH_ARR
JANFEBMARAPRMAYJUN
JULAUGSEPOCTNOVDEC
The first element can be accessed as MNTH_ARR(1).
Run-time array: The array is loaded during the program execution. The code to load the data into the array elements needs to be written.
Example:
DSALES_ARR S 5S 0 DIM(4) ASCEND
Prerun-time array: The array gets loaded from a file when the program is called and before any input, output or calculation operation is performed. FROMFILE keyword is mandatory which specifies the name of the file from which the array will be loaded. TOFILE is optional. If specified, when the program ends, the array data is populated back to the file specified in TOFILE keyword.
Example:
DARH S 5A DIM(250) PERRCD(12) ASCEND
D FROMFILE(DISKOUT) TOFILE(DISKOUT)
Array related Built-in Functions:
%ELEM – Returns the number of elements of an array
%LOOKUP – Used to search for an argument in the array
%XFOOT – Used to find the sum of all the elements of a numeric array
%CONCATARR – Used to concatenate all the elements of an array with common separator
%MAXARR, %MINARR – Used to find index of the max and min values in an array
%LIST – Returns a temporary array with elements passed as arguments to this function
Data Structures:
Data structures allow one to define an area of storage where a set of fields with different lengths and data types can be defined as gets defined in a record. These set of fields are called subfields.
Characteristics:
- The data structure as a whole is considered as a character field, even if the subfields are numeric.
- The subfields are stored in a contiguous memory location
- It is internal to the program like any other stand-alone variable
Data structures are used to:
- Group set of fields and use them as a single field by using the data structure name
- Group fields in non-contiguous memory locations within a data structure to define them in contiguous memory location
- Split a field into multiple subfields
- Redefine a subfield in different ways
- Repeat the same set of subfields multiple times as array of data structures
- Send group of fields as one parameter to the called program
- Perform I/O operations by implicitly moving the data to/from the data structure from/to a record of the database file
Special types of Data Structure
- Data Area Data Structure – Used to automatically read from and write to a data area with data structure content.
- File information Data Structure – System defined data structure which gets automatically updated for every I/O operation to a file to which this data structure is associated. Used to trap the status of the last of I/O operation
- Program Status Data Structure – System defined data structure which gets automatically updated when every statement is executed. Used to trap the status of the last executed statement
- Indicatory Data Structure – Used to give meaningful names for the response and conditioning indicators defined in the display or printer files
Example: To Redefine a variable as an array
**Free
Dcl-ds MonthDs;
Month char(36) inz(‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’)
Monarr char(3) dim(12) overlay(Month);
End-ds;
Dcl-s i int(3);
For i = 1 to %Elem(Monarr);
Dsply Monarr(i);
Endfor;
*inlr = *on;
Example: To split a variable into many subfields
**Free
Dcl-S PartCode char(11) inz(‘CARENG00012’);
Dcl-Ds PartDs;
Assembly Char(3);
Component Char(3);
PartNo Zoned(5) inz(0);
End-Ds;
PartDs = Partcode;
snd-msg ‘Assembly ‘ + Assembly;
snd-msg ‘Component ‘ + Component;
snd-msg ‘Part No ‘ + %char(PartNo);
*inlr = *on;
Using arrays and data structures in RPGLE help to organize data efficiently, making the programs more readable and maintainable. Join our course to learn more about all the types of arrays and data structures