COBOL Tables (OCCURS)
COBOL calls arrays tables. You declare one with the OCCURS clause, which repeats a data item a fixed
number of times. Elements are accessed with a subscript in parentheses, and subscripts start at 1, not 0.
01 WS-MONTHLY-SALES.
05 WS-SALES PIC 9(5)V99 OCCURS 12 TIMES.WS-SALES (1) is January and WS-SALES (12) is December. OCCURS cannot be used on an 01 level: the table
lives inside a group.
Filling and reading a table
IDENTIFICATION DIVISION.
PROGRAM-ID. TABLE-BASICS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SCORES.
05 WS-SCORE PIC 999 OCCURS 5 TIMES.
01 WS-I PIC 9.
01 WS-TOTAL PIC 9(4) VALUE 0.
01 WS-AVERAGE PIC 999V9.
PROCEDURE DIVISION.
MOVE 72 TO WS-SCORE (1)
MOVE 88 TO WS-SCORE (2)
MOVE 95 TO WS-SCORE (3)
MOVE 61 TO WS-SCORE (4)
MOVE 79 TO WS-SCORE (5)
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
DISPLAY "Score " WS-I ": " WS-SCORE (WS-I)
ADD WS-SCORE (WS-I) TO WS-TOTAL
END-PERFORM
DIVIDE WS-TOTAL BY 5 GIVING WS-AVERAGE ROUNDED
DISPLAY "Total " WS-TOTAL ", average " WS-AVERAGE
STOP RUN.Score 1: 072
Score 2: 088
Score 3: 095
Score 4: 061
Score 5: 079
Total 0395, average 079.0
Tables of groups
Each element can be a group, which gives you a table of records:
IDENTIFICATION DIVISION.
PROGRAM-ID. TABLE-OF-RECORDS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CART.
05 WS-ITEM OCCURS 3 TIMES.
10 WS-ITEM-NAME PIC X(12).
10 WS-ITEM-QTY PIC 99.
10 WS-ITEM-PRICE PIC 9(3)V99.
01 WS-I PIC 9.
01 WS-LINE-TOTAL PIC 9(5)V99.
01 WS-CART-TOTAL PIC 9(5)V99 VALUE 0.
01 ED-AMOUNT PIC ZZ,ZZ9.99.
PROCEDURE DIVISION.
MOVE "Coffee beans" TO WS-ITEM-NAME (1)
MOVE 2 TO WS-ITEM-QTY (1) MOVE 12.50 TO WS-ITEM-PRICE (1)
MOVE "Milk" TO WS-ITEM-NAME (2)
MOVE 3 TO WS-ITEM-QTY (2) MOVE 1.20 TO WS-ITEM-PRICE (2)
MOVE "Croissant" TO WS-ITEM-NAME (3)
MOVE 4 TO WS-ITEM-QTY (3) MOVE 2.35 TO WS-ITEM-PRICE (3)
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 3
COMPUTE WS-LINE-TOTAL =
WS-ITEM-QTY (WS-I) * WS-ITEM-PRICE (WS-I)
ADD WS-LINE-TOTAL TO WS-CART-TOTAL
MOVE WS-LINE-TOTAL TO ED-AMOUNT
DISPLAY WS-ITEM-NAME (WS-I) " x" WS-ITEM-QTY (WS-I)
ED-AMOUNT
END-PERFORM
MOVE WS-CART-TOTAL TO ED-AMOUNT
DISPLAY "Total " ED-AMOUNT
STOP RUN.Coffee beans x02 25.00
Milk x03 3.60
Croissant x04 9.40
Total 38.00
Initialising a table with REDEFINES
A table cannot have a VALUE per element in COBOL-85. The classic trick is to write the values as FILLER
items and then redefine the same storage as a table:
IDENTIFICATION DIVISION.
PROGRAM-ID. MONTH-NAMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MONTH-DATA.
05 FILLER PIC X(9) VALUE "January".
05 FILLER PIC X(9) VALUE "February".
05 FILLER PIC X(9) VALUE "March".
05 FILLER PIC X(9) VALUE "April".
01 WS-MONTH-TABLE REDEFINES WS-MONTH-DATA.
05 WS-MONTH-NAME PIC X(9) OCCURS 4 TIMES.
01 WS-M PIC 9 VALUE 3.
PROCEDURE DIVISION.
DISPLAY "Month " WS-M " is " WS-MONTH-NAME (WS-M)
DISPLAY "Month 1 is " WS-MONTH-NAME (1)
STOP RUN.Month 3 is March
Month 1 is January
GnuCOBOL and COBOL 2002 also allow VALUE directly on an OCCURS item, which sets every element to the same
value, for example 05 WS-COUNT PIC 999 OCCURS 7 TIMES VALUE ZERO.
Variable-length tables: OCCURS DEPENDING ON
OCCURS 1 TO 50 TIMES DEPENDING ON WS-COUNT reserves space for 50 elements but treats the table as having
WS-COUNT elements. It is mostly used for variable-length records in files.
IDENTIFICATION DIVISION.
PROGRAM-ID. ODO-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNT PIC 99 VALUE 3.
01 WS-LIST.
05 WS-CODE PIC X(3)
OCCURS 1 TO 10 TIMES DEPENDING ON WS-COUNT.
PROCEDURE DIVISION.
MOVE "AAA" TO WS-CODE (1)
MOVE "BBB" TO WS-CODE (2)
MOVE "CCC" TO WS-CODE (3)
DISPLAY "List: [" WS-LIST "] length "
FUNCTION LENGTH(WS-LIST)
MOVE 2 TO WS-COUNT
DISPLAY "List: [" WS-LIST "] length "
FUNCTION LENGTH(WS-LIST)
STOP RUN.List: [AAABBBCCC] length 000000009
List: [AAABBB] length 000000006
For a variable-length table GnuCOBOL computes FUNCTION LENGTH at run time, and DISPLAY shows that result
with nine digits. The length follows the current value of WS-COUNT: 9 bytes, then 6.
Warning: COBOL does not check subscripts by default.
WS-SCORE (6)in a 5-element table reads whatever bytes follow the table. GnuCOBOL can check at run time withcobc -debug(or-fec=bound-subscript); IBM's equivalent isSSRANGE. Always loop with the declared size.
Test yourself
What is the subscript of the first element of a COBOL table?
- 0
- 1
- -1
- It depends on the OCCURS clause
Why can't you write 01 WS-DAYS PIC 99 OCCURS 7 TIMES.?
- OCCURS is not allowed on level 01
- PIC 99 is too small
- OCCURS needs DEPENDING ON
- You can; it is fine
How do classic programs give each element of a table a different initial value?
- VALUE (1) "A" (2) "B"
- FILLER items with VALUE, redefined as a table
- A LOAD TABLE statement
- It is impossible