|
OCCURS DEPENDING ON
OCCURS DEPENDING ON
Using OCCURS clause we can define tables/arrays in cobol.
For example if you want to store monthly profits for the year,
you need to define 12 data items for each month
01 WS-PROFIT-JAN PIC 9(05)V99.
01 WS-PROFIT-FEB PIC 9(05)V99.
….
01 WS-PROFIT-DEC PIC 9(05)V99.
Instead of declaring multiple data items we can specify field
once and declare that it repeats 12 times, one for each month.
01 WS-PROFIT PIC 9(05)V99 OCCURS 12 TIMES.
WS-PROFIT(1) contain 1st month profit.
WS-PROFIT(2) contain 2nd month profit.
..
WS-PROFIT(12) contain 12th month profit.
Accessing array elements using numbers is called subscripting.
(ie, WS-PROFIT(1) , here 1 is used to refer 1st element in array
, WS-PROFIT(2), here 2 is used to refer 2nd element in array..).
Subscripting is a method of providing table references through
the use of integer numbers as shown in below example.
The OCCURS can also be used at group level.
Example:
01 WS-EMP-DATA OCCURS 20 TIMES.
05 WS-EMP-ID PIC X(05).
05 WS-EMP-NAME PIC X(15).
In the above example OCCURS is at group level so entire group
(employee id and employee name) occurs 20 times.
Table data can also be arranged in ascending or descending order
depending on data items specified. And we can also specify indexes
that can be used with a table.
For example employee data can be arranged in employee id ascending
order, and with index WS-INDEX.
01 WS-EMP-DATA OCCURS 20 TIMES ASCENDING KEY WS-EMP-ID
INDEXED BY WS-INDEX.
05 WS-EMP-ID PIC X(05).
05 WS-EMP-NAME PIC X(15).
OCCURS…. DEPENDING ON:
Variable length tables can be specified using OCCURS DEPENDING ON
clause. This allows tables to occur variable no of times depending
on the value of some other field.
Example:
01 WS-EMP-COUNT PIC 9(03).
01 WS-EMP-DATA OCCURS 1 TO 100 TIMES
DEPENDING ON WS-EMP-COUNT
05 WS-EMP-ID PIC X(05).
05 WS-EMP-NAME PIC X(15).
In above example WS-EMP-DATA is variable length table. WS-EMP-DATA
can have 1 to 100 employee records. The actual number of employee
records depends on WS-EMP-COUNT value.
Size of the table depends on the number of table occurrences, i.e.
WS-EMP-COUNT. Minimum size occupied is 20 bytes and maximum is
2000 bytes.
|
|