Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Pre-Run Time Array in RPG AS400

Pre-Run Time Array in RPG AS400
Pre-Run Time Array in RPG AS400

Introduction to PreRun-Time Array

It is loaded from the array file when the program starts executing.

We can use a Pre-run time array when we want data to be loaded in an array at the program start before any I/O, calculation, or O specs operations and we want to keep array data dynamic.

  • Use DIM and FROMFILE and PERRCD keywords to define the prerun-time array in the RPG program.
  • Define the flat file in F specs having file designation entry set as T which indicates its an array or a table file and File Format as F i.e. program described file and record length of the flat file.
  • In the prerun-time array, we don't need to recompile the RPG program to change the array elements data which is an advantage over the compile-time array.
  • Steps to code PreRun-Time Array

  • Create a flat file with Record length and no DDS.
  • Insert array elements in the flat file to be used in the RPG program.
  • Write an RPG program to demonstrate the use of the PreRun-Time array using a Flat file.
  • Create the program using option 14.
  • Run the program to see the output.
  • Create a Flat file

    Let's create two flat files namely FLATFILE1 and FLATFILE2 with record length 112. Use the CRTPF command as below to create the FLATFILE1 and FLATFILE2 objects in your current library.

    CRTPF FILE(FLATFILE1)
          RCDLEN(112)    
    CRTPF FILE(FLATFILE2)
          RCDLEN(112)    

    Inserted Array element records in Flat file

    You can insert data using SQL INSERT from STSRQL and UPDDTA command as below in FLATFILE1 and FLATFILE2.

  • FLATFILE1 records
  • FLATFILE1                       
    AJAY                            
    AMIT                            
    AMAR                            
    ABHAY                           
    ********  End of data  ******** 
  • FLATFILE2 records
  • FLATFILE2                       
    AJAY      SINGH                 
    AMIT      KUMAR                 
    AMAR      PRATAP                
    ABHAY     SHARMA                
    ANKUR     SINGH                 
    ********  End of data  ******** 

    Coding a PrRun-Time Array in RPGLE

    Let's write a program for PreRun-Time Array in RPGLE fixed, free, and fully free format.

    RPG Code in Fixed format for PreRun Time Array in AS400
          *Header Specification                                                 
         HDebug(*Yes)                                                               
         HOption(*NoDebugio)                                                        
         FFLATFILE1 IT   F  112        DISK                                         
         FFLATFILE2 IT   F  112        DISK                                         
          * program variables                                                       
         D PreRunTimeArray1...                                                      
         D                 S             10A   DIM(5) FROMFILE(FLATFILE1) PERRCD(1) 
         D PreRunTimeArray2...                                                      
         D                 S             10A   DIM(10) FROMFILE(FLATFILE2) PERRCD(2)
          *                                                                         
         D Index           S             10i 0                                      
         D AddArrayData1   S             10A                                        
         D AddArrayData2   S             10A                                                                              
          *                                                                     
         C                   EVAL      Index = 1                                
         C                   DOW       Index <=5                                
         C                   EVAL      ArrayData1 =                            
         C                             %Trim(PreRunTimeArray1(Index))       
         C     ArrayData1    DSPLY                                              
         C                   EVAL      Index = Index + 1                        
         C                   ENDDO     
         C                   EVAL      Index = 1       
         C                   DOW       Index <=10                                
         C                   EVAL      ArrayData2 =                            
         C                             %Trim(PreRunTimeArray2(Index))       
         C     ArrayData2    DSPLY                                              
         C                   EVAL      Index = Index + 1                        
         C                   ENDDO                        
         C                   EVAL      *INLR = *ON                                                    
    
    RPG Code in /Free format for PreRun Time Array in AS400
          *Header Specification                                                     
         HDebug(*Yes)                                                               
         HOption(*NoDebugio)                                                        
         FFLATFILE1 IT   F  112        DISK                                         
         FFLATFILE2 IT   F  112        DISK                                         
          * program variables                                                       
         D PreRunTimeArray1...                                                      
         D                 S             10A   DIM(5) FROMFILE(FLATFILE1) PERRCD(1) 
         D PreRunTimeArray2...                                                      
         D                 S             10A   DIM(10) FROMFILE(FLATFILE2) PERRCD(2)
          *                                                                         
         D Index           S             10i 0                                      
         D AddArrayData1   S             10A                                        
         D AddArrayData2   S             10A                                        
          /Free                                                                     
           // Begin program                                                         
           Index =  1;                                                              
           Dow (Index <=5);                                                         
             AddArrayData1= %Trim(PreRunTimeArray1(Index)) ;                        
             DSPLY AddArrayData1;                             
             Index = Index + 1;                               
           EndDo;                                             
                                                              
           Index =  1;                                        
           Dow (Index <=10);                                  
             AddArrayData2= %Trim(PreRunTimeArray2(Index)) ;  
             DSPLY AddArrayData2;                             
             Index = Index + 1;                               
           EndDo;                                             
           //Set Last Record Indicator ON                     
           *Inlr = *ON;                                       
                                                              
          /End-Free                                                                                                                                                                      
    
    File Designation T is not supported in Fully free RPG. Therefore, I am not writing fully free code for PreRun-Time Array.
                                                                                                                                                                             
    

    Please note that PERRCD concept can be referred from here.In PreRun-Time Array case the record referred from flat file.

    Create the RPGLE object of SQLRPGLE source member

    Use CRTSQLRPGI command or option 14 on the RPG source member using WRKMBRPDM.

                         Create SQL ILE RPG Object (CRTSQLRPGI)                      
                                                                                     
     Type choices, press Enter.                                                      
                                                                                     
     Object . . . . . . . . . . . . . OBJ          > ARRAYPRE                        
       Library  . . . . . . . . . . .              >   IBMICLASS1                    
     Source file  . . . . . . . . . . SRCFILE      > QRPGLESRC                       
       Library  . . . . . . . . . . .              >   IBMICLASS2                    
     Source member  . . . . . . . . . SRCMBR       > ARRAYPRE                        
     Source stream file . . . . . . . SRCSTMF                                        
                                                                                     
     Commitment control . . . . . . . COMMIT         *CHG                            
     Relational database  . . . . . . RDB            *LOCAL                          
     Compile type . . . . . . . . . . OBJTYPE      > *PGM                            
     Listing output . . . . . . . . . OUTPUT         *NONE                           
     Text 'description' . . . . . . . TEXT           *SRCMBRTXT                      
                                                                                     

    An object is created as follows

                                  Work with Objects                              
                                                                                 
    Type options, press Enter.                                                   
      2=Edit authority        3=Copy   4=Delete   5=Display authority   7=Rename 
      8=Display description   13=Change description                              
                                                                                 
    Opt  Object      Type      Library     Attribute   Text                      
         ARRAYPRE    *PGM      IBMICLASS1  RPGLE       Pre Run Time Array        

    Program Output

    DSPLY  AJAY   
    DSPLY  AMIT   
    DSPLY  AMAR   
    DSPLY  ABHAY  
    
    DSPLY  AJAY    
    DSPLY  SINGH   
    DSPLY  AMIT    
    DSPLY  KUMAR   
    DSPLY  AMAR    
    DSPLY  PRATAP  
    DSPLY  ABHAY   
    DSPLY  SHARMA  
    DSPLY  ANKUR   

    Related Post

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain