Skip to content
OCCURS — Arrays
step 1/5

Reading — step 1 of 5

Learn

~1 min readStructured Data

COBOL arrays use OCCURS. Fixed-size at compile time, indexed from 1.

       01 GRADES.
           05 GRADE PIC 9(3) OCCURS 5 TIMES.

This declares 5 grade slots, accessed as GRADE(1) through GRADE(5).

Iteration with PERFORM VARYING:

       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
           DISPLAY GRADE(I)
       END-PERFORM.

The index variable (I) needs to be declared as numeric: 01 I PIC 9(2).

Multi-dimensional:

       01 BOARD.
           05 ROW OCCURS 8 TIMES.
               10 CELL OCCURS 8 TIMES PIC X.

*> Access:
       MOVE "X" TO CELL OF ROW(3) (5).
       *> or simpler:
       MOVE "X" TO CELL(3, 5).

OCCURS DEPENDING ON — variable-length arrays:

       01 ITEM-COUNT  PIC 9(3).
       01 ITEMS.
           05 ITEM PIC X(20) OCCURS 1 TO 100 TIMES
                            DEPENDING ON ITEM-COUNT.

The array size is determined by ITEM-COUNT at runtime. Common in records read from files.

Sorting an OCCURS array — use SORT (file-based) or INSPECT/manual algorithms. Most COBOL code sorts via SORT (file-level, very fast in mainframe COBOL).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…