Back to: ILE RPG in Easy Bytes

In Unit 10 you learn how to process arrays, which are data structures that can simplify repetitive processing of similar data. In addition, this unit discusses how to define arrays, populate them with data, and access that data. It also covers the use of arrays and array data structures together with SQL.
This course is a unit of the ILE RPG in Easy Bytes series, a complete introductory ILE RPG programming self-guided tutorial.
- Before starting this course you must complete the required prerequisite course: 9. Embedded SQL – ILE RPG in Easy Bytes
One-Time Payment
Members Only
Instead of paying for each unit individually, you may purchase a membership which will automatically enroll you at a discount in the entire “ILE RPG in Easy Bytes” series.
Defining and Processing Arrays
Similar kinds of data are stored within a column, and the data elements within a row of the table are related, or belong together. Typically, the data elements in the first column of a table are organized sequentially in ascending order to facilitate finding an item. Once you find the item you want in column 1, you then read across the row to extract the data related to that item.
The array data must be entered at the end of the program, following the last program entries. ILE RPG uses a **Ctdata record as a delimiter (or separator line) to explicitly identify the array whose data follows. To code this delimiter, place asterisks (**) in positions 1 and 2 of a line following the last line of program code; in positions 3–8, enter Ctdata (for compile time data); leave position 9 blank; and, starting in position 10, enter the name of the array whose data follows.
To indicate an array element in ILE RPG, you use the array name followed by a set of parentheses that contains the ordinal location of the element within the array. The index numbering starts with 1. Thus, Days(3) means the third element in the array Days, and Taxrate(10) is the tenth element in the array Taxrate.
The %Lookup functions take the following form: %Lookupxx(search-argument : array {:
The function’s only argument contains the name of the array whose elements are to be added together. Use an assignment expression to name the field where you want to store the answer. If you half-adjust this operation, the rounding occurs just before the final answer is stored.
An array data structure is a data structure defined with the Dim keyword. By defining an array as a subfield within an array data structure, a program can effectively define a two-dimensional array. Consider the following declarations:
At IBMi Release 7.4, ILE RPG can define varying-dimension arrays. There are two types of dynamic arrays: DIM(*AUTO) and DIM(*VAR).
This type of Fetch is called a single fetch. If the host structure is an array data structure, Fetch can perform a multiple-row fetch, retrieving several rows with one statement and placing the result set into individual elements of the array data structure. The Fetch statement for a multiple-row fetch takes the following form:
Fixed-format Calculation specifications also support a Lookup operation code that performs similarly in legacy RPG programs. The Lookup operation takes the following form: *..
- Before starting this course you must complete the required prerequisite course: 9. Embedded SQL – ILE RPG in Easy Bytes