Arrays in ILE RPG (RPG IV) Run Time Arrays, Compile Time Arrays
Arrays in RPGLE
In RPG-ILE, we have arrays to store similar types of variables. An array is declared by the keyword Dimension. We specify the size of array as the parameter of the Dim() keyword. A typical array will be declared as below.
Two types of Arrays in RPGLE
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords DW@RTime S 5P 0 Dim(5)
Just like any other programming lanuage, the arrays in RPGLE are referenced using their index. To access the first element of the above array we have to use W@RTime(1), and so on. This will be explained by the example below which first populates the array and then displays the value of each array element one by one.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++
DW@RTime S 5P 0 Dim(5) Inz D @Indx S 1P 0 Inz ** Populate the error CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++ C Do 5 @Indx C Eval W@RTime(@Indx) = @Indx + 200 C EndDo ** Access the array elements C Do 5 @Indx C W@RTime(@Indx)Dsply C EndDo ** C Return
The output of the above example is given below.
DSPLY 201 DSPLY 202 DSPLY 203 DSPLY 204 DSPLY 205
Two types of Arrays in RPGLE
In ILE RPG we've two types of arrays.
- Runtime Array
- Compile time Array
The array we have just seen is a run time array. As the array elements are populated at the run time. In the case of compile time arrays, the arrays must be populated at the time of compilation. The compile time arrays of RPGLE are used mainly to store error messages. Though this is not limited to error message population only. The data of a compile time array is defined at the end of all the source codes at the bottom. The compile time array is declared as below.
Example of a compile time array
Example of a compile time array
.....DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++ DW@RTime S 5P 0 Dim(5) CTDATA D @Indx S 1P 0 Inz ** Access the array elements .....CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+ C Do 5 @Indx C W@RTime(@Indx)Dsply C EndDo ** C Return **CTDATA W@RTime 00101 00102 00103 00104 00105
Notice the initialization of the array elements. The **CTDATA specifies that the array elements of the array W@RTime are being declared below this line. If you have to specify multiple compile time array then you should specify this for each of compile time arrays. The output of the above program is given below.
DSPLY 101 DSPLY 102 DSPLY 103 DSPLY 104 DSPLY 105
Notice the difference between the declarations of the compile and runtime arrays. In compile time arrays we have to specify another keyword CTDATA alongwith the dimension. This keyword specifies that the array stores Compile Time DATA.
We can define multiple array elements in one line. For this we have to specify that there are multiple subrecords in each of records at the bottom. We do this using the keyword PerRcd. Notice that the second record should begin immediately after the first record. i.e if the size of each of the array elements is defined as 10A. then the second element of this array should begin exactly at 11th position on the record. Though you may keep it blank(Which is valid value)
The example below demonstrates a compile time array with multiple array elements defined in each records.
Example showing the PerRcd and CTDATA keywords on a compile time array.
.....DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++ DW@RTime S 5P 0 Dim(4) CTDATA PerRcd(2) D @Indx S 1P 0 Inz ** Access the array elements .....CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len C Do 4 @Indx C W@RTime(@Indx)Dsply C EndDo ** C Return **CTDATA W@RTime 0010100102 0010300104
The output of the above example is as given below. Here, notice how the array data is
given. The first data occupies the first five positions of the record. Immediately after this, the second array element's value follows. Similary the rest of twovalues have been populated from the second record.
given. The first data occupies the first five positions of the record. Immediately after this, the second array element's value follows. Similary the rest of twovalues have been populated from the second record.
DSPLY 101 DSPLY 102 DSPLY 103 DSPLY 104
- 25832 reads
RPG IV (RPG ILE) and AS400 Data Structure Programming
Purpose of this article is to learn Data Structures and their common usage. We will also learn Program Status and File Information data structures. First thing first, let us learn
What is a data structure?
Data structure is in a sense a collection of several data types. Most of the times a data structure in RPG IV, in whole, is of character type.
Data structures give us an easy means to treat different portions of an RPG variable as different data types' variables.
Data structures in RPG IV are most commonly used for
- Date conversions:- Though not recommended, because they seem to be very complex and clumsy, they are found to be effective performers as compared to built in functions
- Processing flat files:- When dealing with several other systems, data transfer from and to an AS400 machine is mostly in flat file format. It seems XML is still to be popular here! And to process a flat file, it seems, there is no other alternative of data structures.
Basic idea behind using data structures in RPG is to treat and use different portions of a single variable as different variables.
Defining a data structure on AS400 system
Data structures are defined in D-Specs in RPG IV. In RPG, Data structures were defined in I spec though. Since RPG IV still supports RPG III type data structure declaration, some programmers use this way only. However, I will use only D-Spec type declaration only.
The syntax of RPG data structure is as below.
D Name_of_DS DS
D Field1 Length
D Field2 Length2
D Field3 Length3
Example of an RPG IV data structure
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++
D W@Name DS
D First 1 8
D Last 9 14
*
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+
C *Entry PList
C Parm W@Name
C First Dsply
C Last Dsply
C Return
In the example above, the entry parameter is supposed to be concatenation of First and last names of a person.
When we call the program with parameter 'TutorialIndia' we receive the following output.
DSPLY Tutorial
DSPLY India
So, this is an easy way to accomplish functionality similar to %SubSt function !.
However, this is not the only purpose of a data structure. Data structures are found to be more useful for clubbing different data types' variables together.
In the example below, we pass a student's name and marks obtained in two papers. The program will then display the name and average of the marks.
In the example below, we pass a student's name and marks obtained in two papers. The program will then display the name and average of the marks.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++
D W@Name_Marks DS
D Name 1 8
D Sub1 9 10 0
D Sub2 11 12 0
D Avg S 2 0
*
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++
C *Entry PList
C Parm W@Name_Marks
C Eval Avg = (Sub1 + Sub2) / 2
C Name Dsply
C Avg Dsply
C Return
When I called this program with 'Jones 6896' as parameter, I got the following output
DSPLY Jones
DSPLY 47
Notice that since first 8 spaces of the data structure are declared to be character hence I have to fill the remaining spaces of this portion with blanks. We always have to take care of this thing. Also, since rest of the portion is declared to be numeric, we can not fill characters here. Otherwise we have to face the dreaded decimal data error.
I think, you get the point that the data structure has enabled us to use sub1 and sub2 parameters as different numeric variables and we were able to perform mathematical operations on them!
Initialize a data structure
We can either initialize each part of a data structure separately or put the keyword INZ at the data structure level to initialize each field of the data structure at a time.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++
D W@Ds DS Inz
Note: - Since initializing an entry parameter is not allowed, you should not try to initialize the data structures in the two examples given above.
Hi Hari,
ReplyDeleteThe articles you gave us very good.
Could you please suggest few programs for us to practice?
Regards,
Rama Krishna
Can you please explain with example in real time use
ReplyDelete