INDEXED BY and OCCURS DEPENDING ON
INDEXED BY
INDEXED BY adds an index register to the table. The index holds the entry number in a form the CPU can use directly, which is faster than a subscript on large tables:
01 WS-STAFF.
05 WS-NAME PIC X(30)
OCCURS 100 TIMES
INDEXED BY WS-STAFF-IX.
SET and indexes
Indexes move with SET, not MOVE. SET UP and SET DOWN step by one, and you can assign an index from another index or a literal:
SET WS-STAFF-IX TO 1.
SET WS-STAFF-IX UP BY 1.
SET WS-STAFF-IX DOWN BY 2.
OCCURS DEPENDING ON
OCCURS DEPENDING ON makes the used length depend on a variable. The table reserves its maximum size, but loops and FILE records only see the used entries:
01 WS-ROW-COUNT PIC 9(2) VALUE 0.
01 WS-ROWS.
05 WS-ROW OCCURS 1 TO 50 TIMES
DEPENDING ON WS-ROW-COUNT PIC X(20).
The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev