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
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.
C OCCUR DS1 resultfield
Current occurrence of multiple occurrence data structure DS1 is placed in resultfield.
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
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
%OCCUR function
%OCCUR sets or gets the current occurrence of the multiple occurrence data structures.
D DS1 DS OCCURS(5) Doccurrence S 3p 0 /FREE occurrence = %OCCUR(DS1); /END-FREE
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
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
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
**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;
Program Output
DSPLY TESTA1 TESTB1 TESTC1 DSPLY TESTA2 TESTB2 TESTC2 DSPLY TESTA3 TESTB3 TESTC3 DSPLY TESTA4 TESTB4 TESTC4 DSPLY TESTA5 TESTB5 TESTC5
Related Post
- Data Structure and Types of DS in RPG AS400
- Using a Data Structure to subdivide the field in RPG AS400
- Using a Data Structure to group fields in RPG AS400
- Externally Described Data Structure in RPG AS400
- Using EXTFLD to code Externally Described DS in RPG AS400
- Using PREFIX to rename all fields in an external data structure in RPG AS400
- Define an externally-described data structure using the LIKEREC keyword in RPG AS400
- Difference between LIKEREC and EXTNAME keyword in RPG AS400
- Data Area Data Structure in RPG AS400
- *LDA Local data area data structure in RPG AS400
- File information data structures (INFDS) in RPG AS400
- Indicator data structure in RPG AS400
- Program Status Data Structure in RPG AS400
- Using keywords QUALIFIED, LIKEDS, and DIM with data structures
- Array Data Structures in RPG AS400
- Defining Data Structure Parameters in a Prototype or Procedure Interface