Multiple Occurrence Data Structure in RPG AS400

Multiple Occurrence Data Structure in RPG AS400
Multi Occur data structure,Multiple Occurrence Data Structure in RPG AS400, %OCCUR, OCCURS, DS ,data structure,Set/get occurrence of a data structure,Multiple Occurrence Data Structure in RPGLE,OCCUR opcode in rpgle, what, make, create, about, introduction
Multiple Occurrence Data Structure in RPG AS400

What is Multiple Occurrence Data Structure

The OCCUR opcode or %OCCUR function can be used to change the current occurrence of the data structure to be used next in an RPG program. Initially, the occurrence is set to 1 and then it can be changed either using OCCUR opcode or %OCCUR function. Only one occurrence can be used at a time.

If the subfield of a multiple occurrence data structure is specified in any operation then the first occurrence of the data structure is used until an OCCUR operation is specified. After OCCUR operation, the occurrence specified will be used.

OCCUR opcode

Code Factor 1 Factor 2 Result Field Indicators
OCCUR Occurrence Value Data Structure Occurrence Value __ ER __

Factor 1

  • Factor 1 is optional.
  • Possible values are numeric, field, named constant, data structure name.
  • Factor 1 is used during the OCCUR operation to set the occurrence of the data structure specified in Factor 2.
  • C     2             OCCUR     DS1

    Multiple occurrence data structure DS1 is set to occurrence 2 specified in Factor 1.

    C     Factor1       OCCUR     DS1

    Multiple occurrence data structure DS1 is set to occurrence value of Factor1 variable specified in Factor1.

  • When Factor 1 is not specified means it is blank then the value of the current occurrence of the data structure specified in Factor 2 is placed in the result field during the OCCUR operation.
  • C                   OCCUR     DS1           resultfield

    Current occurrence of multiple occurrence data structure DS1 is placed in resultfield.

  • If we specify a data structure name in Factor 1 then that data structure must be a multiple occurrence data structures. The current occurrence of the data structure specified in Factor 1 is used to set the occurrence of the data structure specified in Factor 2.
  • C     DS2           OCCUR     DS1

    multiple occurrence data structure DS1 occurrence is set to the current occurrence of the multiple occurrence data structure DS2.

    Factor 2

  • Factor 2 is mandatory.
  • It must contain the name of the multiple occurrence data structures.
  • Result Field

  • Result Field is optional.
  • It must contain the numeric data type field with zero decimal places.
  • During the occur operation the value of the current occurrence of the data structure specified in Factor 2 is placed in the result field.
  • At least one, either Factor 1 or Result field has to be specified in the OCCUR operation.

    C     DS2           OCCUR     DS1           resultfield

    DS1 and DS2 would be multiple occurrence data structures. Here DS1 is set to the current occurrence of the DS1 and the current occurrence of DS1 is placed in resultfield.

    Indicators

  • Error Indicator ER is used to catch exceptions during OCCUR operation. Please note that we can also use operation code extender E as well to handle OCCUR exceptions.
  • %OCCUR function

    %OCCUR sets or gets the current occurrence of the multiple occurrence data structures.

  • Specified this function as an evaluated value i.e. on the right-hand side on EVAL statement will return the current occurrence of the multiple occurrence data structures.
  •      D DS1             DS                  OCCURS(5)
         Doccurrence       S              3p 0   
          /FREE
            occurrence = %OCCUR(DS1);
          /END-FREE
    
  • Specified this function on the left-hand side of an EVAL statement to set the occurrence specified on the right-hand side of the EVAL statement.
  •      D DS1             DS                  OCCURS(5)
         Doccurrence       S              3p 0   
          /FREE
            %OCCUR(DS1) = 3;
          /END-FREE
    

    Declaring Multiple Occurrence data Structure in Fixed Format RPG

    DS1 is a multiple occurrence data structure and has 5 occurrences.

         D DS1             DS                  OCCURS(5)
         D subfld1                 1     10                 
         D subfld2                11     20                 
    

    Or

         D MAXOCCURRENCE   C                   CONST(5)
         D DS1             DS                  OCCURS(MAXOCCURRENCE)
         D subfld1                 1     10                 
         D subfld2                11     20                 
    

    Declaring Multiple Occurrence data Structure in Fully Free-Format RPG

    DS1 is a multiple occurrence data structure and has 5 occurrences.

    dcl-ds ds1 OCCURS(5);     
     subfld1 char(10) pos(1);   
     subfld2 char(10) pos(11);  
    end-ds;                     
    

    Or

    dcl-c MAXOCCURRENCE CONST(5);
    dcl-ds ds1 OCCURS(MAXOCCURRENCE);     
     subfld1 char(10) pos(1);   
     subfld2 char(10) pos(11);  
    end-ds;                         
    

    Here, CONST keywors in optional to use.

    Coding multiple occurrence data structure in RPGLE Fixed, /Free and Fully Free formats

    RPG Code in Fixed format for Multiple occurrence data structure in RPGLE AS400
         D DS1             DS                  OCCURS(5)                        
         D subfld1                 1     10                                     
         D subfld2                11     20                                     
         D subfld3                21     30                                     
         Dfactor1          S              3p 0                                  
         Dresultfield      S              3p 0                                  
         C                   EVAL      factor1 = 1                              
         C                   EVAL      resultfield = 0                          
         C     factor1       DO        5                                        
         C     factor1       OCCUR     DS1           resultfield                
         C                   EVAL      subfld1 = 'TESTA' + %char(resultfield)   
         C                   EVAL      subfld2 = 'TESTB' + %char(resultfield)   
         C                   EVAL      subfld2 = 'TESTC' + %char(resultfield)   
         C                   EVAL      factor1 = factor1 + 1                    
         C     DS1           DSPLY                                              
         C                   ENDDO                                              
         C                   SETON                                        LR                                                 
    
    RPG Code in /Free format for Multiple occurrence data structure in RPGLE AS400
         D DS1             DS                  OCCURS(5)      
         D subfld1                 1     10                   
         D subfld2                11     20                   
         D subfld3                21     30                   
         Dfactor1          S              3p 0                
         Dresultfield      S              3p 0                
          /Free                                               
           factor1= 1;                                        
           resultfield = 0;                                   
           for factor1 = 1 BY 1 to 5 ;                        
             %OCCUR(DS1)  = factor1;                          
             resultfield = %OCCUR(DS1);                       
             subfld1 = 'TESTA' + %char(resultfield);          
             subfld2 = 'TESTB' + %char(resultfield);          
             subfld3 = 'TESTC' + %char(resultfield);          
             dsply DS1;                                       
           endfor;                                            
           *INLR = *ON;                                       
          /End-Free                                                                                                                             
    
    RPG Code in Fully Free format for Multiple occurrence data structure in RPGLE AS400
    **FREE                                   
    dcl-ds ds1 occurs(5);                    
     subfld1 char(10) pos(1);                
     subfld2 char(10) pos(11);               
     subfld3 char(10) pos(21);               
    end-ds;                                  
                                             
    dcl-s factor1 packed(3:0);               
    dcl-s resultfield packed(3:0);           
                                             
    factor1= 1;                              
    resultfield = 0;                         
    for factor1 = 1 BY 1 to 5 ;              
      %OCCUR(DS1)  = factor1;                
      resultfield = %OCCUR(DS1);             
      subfld1 = 'TESTA' + %char(resultfield);
      subfld2 = 'TESTB' + %char(resultfield);
      subfld3 = 'TESTC' + %char(resultfield);
      dsply DS1;                             
    endfor;       
    *INLR = *ON;                      
    
  • In Fixed format RPG, we used OCCUR operation code for setting data structure DS1 occurrences from FACTOR1 value in DO loop from 1 till 5 since the DS1 has 5 occurrences.
  • In /free or Fully Free RPGLE, we used the %OCCUR function instead of the OCCUR opcode as OCCUR opcode can only be used in Fixed format RPG. Also, the DO loop can only be used in Fixed format. Therefore, we used FOR loop in place of DO in /Free or Fully Free RPG.
  • Program Output

    DSPLY  TESTA1    TESTB1    TESTC1
    DSPLY  TESTA2    TESTB2    TESTC2
    DSPLY  TESTA3    TESTB3    TESTC3
    DSPLY  TESTA4    TESTB4    TESTC4
    DSPLY  TESTA5    TESTB5    TESTC5
    

    Related Post

    Post a Comment

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