COBOL - Occurs Depending on Clause
If you do not know before run time how many times a table element occurs, define a variable-length table. To do so, use the OCCURS DEPENDING ON (ODO) clause.
X OCCURS 1 TO 10 TIMES DEPENDING ON Y |
In the example above, X is called the ODO subject, and Y is called the ODO object.
Two factors affect the successful manipulation of variable-length records:
- Correct calculation of record lengths - The length of the variable portions of a group item is the product of the object of the DEPENDING ON phrase and the length of the subject of the OCCURS clause.
- Conformance of the data in the object of the OCCURS DEPENDING ON clause to its PICTURE clause -If the content of the ODO object does not match its PICTURE clause, the program could terminate abnormally. You must ensure that the ODO object correctly specifies the current number of occurrences of table elements.
Example 1:
05 LINE-ITEM-COUNT PIC 99.
05 LINE-ITEMS OCCURS 0 TO 25 TIMES DEPENDING ON LINE-ITEM-COUNT.
10 QUANTITY PIC 9999.
10 DESCRIPTION PIC X(30).
10 UNIT-PRICE PIC S9(5)V99.
|
The table LINE-ITEMS contains three fields (QUANTITY, DESCRIPTION, UNIT-PRICE) which make up one line on an invoice.
Each invoice can have from 0 to 25 line items. The actual number of line items on a particular invoice, and therefore in the record, is given by the field LINE-ITEM-COUNT.
One instance of the table occupies 41 bytes, and the table may occur from 0 to 25 times, so the total space occupied by the table LINE-ITEMS in the record varies from 0 bytes to 1025 bytes. If there are any fields after this table, their position in the record will shift, depending on the number of occurrences of the table.
Example 2:
01 REC-1.
05 FIELD-1 OCCURS 1 TO 100 TIMES DEPENDING ON WS-COUNT.
10 WS-A PIC X(05).
10 WS-B PIC X(05).
|
In the above example FIELD-1 is a variable length table. FIELD-1 can have 1 to 100 records. The actual number of records depends on WS-COUNT value.
Size of table is depends on the number of table occurances, i.e. WS-COUNT. Minimum size is occupied 10 bytes and maximum is 1000 bytes.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!