Saturday 24 March 2012


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.
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
.....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.
DSPLY    101
DSPLY    102
DSPLY    103
DSPLY    104

2 comments:

  1. Hi Hari,

    The articles you gave us very good.
    Could you please suggest few programs for us to practice?

    Regards,
    Rama Krishna

    ReplyDelete
  2. Can you please explain with example in real time use

    ReplyDelete