Array Data Structures in RPG AS400

Array Data Structures in RPG AS400
Array Data Structures in RPG AS400, array ds, data structure, DIM, array in rpgle, array data structure in rpgle
Array Data Structures in RPG AS400

Introduction to Array Data Structure

It is a data structure defined with the keyword DIM. An Array data structure is like a multiple occurrence data structure except that the index has to be specified for Arrays.

The syntax for Array Data Structure Declaration

  • In Fixed format RPG
  •      D DS1             DS                  QUALIFIED DIM(2)  
         D subfld1                       10                      
         D subfld2                        5                      
         D subfld3                        5                      
    
  • In Fully Free-format RPG
  • **FREE
    dcl-ds ds1 qualified dim(2);
     subfld1 char(10);
     subfld2 char(5);
     subfld3 char(5);
    end-ds ds1;
    

    Coding Array Data Structure in RPG AS400

    RPG Code in Fixed format for Array Data Structures in RPG AS400
         D DS1             DS                  QUALIFIED DIM(2)                
         D subfld1                       10                                    
         D subfld2                        5                                    
         D subfld3                        5                                    
                                                                               
                                                                               
         C                   EVAL      DS1(1).subfld1 = 'DS1_1'                
         C                   EVAL      DS1(1).subfld2 = 'DS1_2'                
         C                   EVAL      DS1(1).subfld3 = 'DS1_3'                
                                                                               
         C     ds1(1)        DSPLY                                             
         C                   EVAL      DS1(2).subfld1 = 'DS1_4'                
         C                   EVAL      DS1(2).subfld2 = 'DS1_5'                
         C                   EVAL      DS1(2).subfld3 = 'DS1_6'                
         C     ds1(2)        DSPLY                                             
         C                   SETON                                        LR     
    
    RPG Code in /Free format for Array Data Structures in RPG AS400
         D DS1             DS                  QUALIFIED DIM(2)  
         D subfld1                       10                      
         D subfld2                        5                      
         D subfld3                        5                      
          /Free                                                  
           DS1(1).subfld1 = 'DS1_1';                             
           DS1(1).subfld2 = 'DS1_2';                             
           DS1(1).subfld3 = 'DS1_3';                             
           dsply ds1(1);                                         
                                                                 
           DS1(2).subfld1 = 'DS1_4';                             
           DS1(2).subfld2 = 'DS1_5';                             
           DS1(2).subfld3 = 'DS1_6';                             
           dsply ds1(2);                                         
                                                                 
           *INLR = *ON;                                          
          /End-Free                                                                                                                          
    
    RPG Code in fully Free format for Array Data Structures in RPG AS400
    **FREE                        
    dcl-ds ds1 qualified dim(2);  
     subfld1 char(10);            
     subfld2 char(5);             
     subfld3 char(5);             
    end-ds;                       
    DS1(1).subfld1 = 'DS1_1';     
    DS1(1).subfld2 = 'DS1_2';     
    DS1(1).subfld3 = 'DS1_3';     
    dsply ds1(1);                 
                                  
    DS1(2).subfld1 = 'DS1_4';     
    DS1(2).subfld2 = 'DS1_5';     
    DS1(2).subfld3 = 'DS1_6';     
    dsply ds1(2);                 
                                  
    *INLR = *ON;                                                
    
  • Here in the above example, data structure DS1 is defined as Qualified and with the DIM keyword. Therefore DS1 is an Array data structure. We are referencing the ds subfields using the ds array element.
  • Output

    DSPLY  DS1_1     DS1_2DS1_3   
    DSPLY  DS1_4     DS1_5DS1_6   
    

    Related Post

    Post a Comment

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