ibm-midrangerpgle

RPGLE data structure features?


I am playing with RPGLE data structure features. We store 5 Clerks and 5 Managers in a data area.
then we want to bring them into and RPG program and chop them into
two strings suitable fore SQL Where clause. This seems a bit wordy. Can I do the same with less lines? Code below. Simplified example.

DCL-DS info2 DTAARA('*LIBL/MYDTAARA');                                                 
    dcl-subf DTA       char(300);                                                      
    dcl-subf Manager  char(50) overlay(DTA:51) ;                                      
    dcl-subf Manager1 char(10) overlay(Manager:01) ;                                  
    dcl-subf Manager2 char(10) overlay(Manager:11) ;                                  
    dcl-subf Manager3 char(10) overlay(Manager:21) ;                                  
    dcl-subf Manager4 char(10) overlay(Manager:31) ;                                  
    dcl-subf Manager5 char(10) overlay(Manager:41) ;                                  
    dcl-subf Clerk  char(50) overlay(DTA:201) ;                                  
    dcl-subf Clerk1 char(10) overlay(Clerk:01) ;                                
    dcl-subf Clerk2 char(10) overlay(Clerk:11) ;                                
    dcl-subf Clerk3 char(10) overlay(Clerk:21) ;                                
    dcl-subf Clerk4 char(10) overlay(Clerk:31) ;                                
    dcl-subf Clerk5 char(10) overlay(Clerk:41) ;                                
    dcl-subf Managers Overlay(Manager)  Like(Manager1) Dim(5);                      
    dcl-subf Clerks Overlay(Clerk) Like(Clerk1) Dim(5);                   
 End-ds ;  
         


 Dcl-s sCLERK Char(80)   ;            
 Dcl-s sMANAGER Char(80)   ;    
 Dcl-s x   Int(2)        ;             
 Dcl-s C   CHAR(1)       ;   // comma  
 Dcl-c Q   CONST('''')   ;   // quote  

For x = 1 to 5 by 1;                                                   
 If Clerks(x) <> *blanks ;                                        
    sCLERK  =  %trim(sCLERK)  + C + Q + %trim(Clerks(x)) + Q;     
    C =',';                                                           
 EndIf ;                                                              
EndFor;                                                                
C =' ';                                                                
For x = 1 to 5 by 1;                                                   
  If Managers(X) <> *blanks ;                                      
    sMANAGER  =  %trim(sMANAGER) + C + Q + %trim(Managers(x)) + Q;      
    C =',';                                                            
  EndIf ;                                                              
EndFor;                                                                                                

Solution

  • As asked (Can I do the same with less lines?) the answer is... not really. This is how a data structure works; it's a map of a shared chunk of data that one wants to give individual sections a name.

    That said, a data structure is only one choice. If one wants to perform a similar function in fewer lines, consider eliminating the data structure altogether. In the loops, substring the individual clerks and managers out of the larger string. This code isn't using clerk1...5 - they are only there to be able to overlay an array over the top of the data area.

    If the reason for the data area is to pass these from another program, consider using CALL/PARM rather than a data area. Directly pass the two arrays. That will remove the lines declaring the data area from this program, and avoid having to format the data area in the calling program.