COBOLintermediate

Tables and OCCURS Clause

Understand how COBOL implements arrays via OCCURS and how INDEXED BY drives efficient table access.

A COBOL table is an array. It is declared with OCCURS.

A COBOL table is a wall of labeled pigeonholes fixed in place at compile time — INDEXED BY gives you a fast pointer finger that jumps straight to a slot instead of counting from the first pigeonhole every time.

Key Concepts

1
    05  RATE-TABLE  OCCURS 50 TIMES INDEXED BY RT-IDX.
        10  RT-CODE   PIC X(4).
        10  RT-AMOUNT PIC S9(7)V99 COMP-3.
2
Fifty copies of that group, laid out contiguously, reached as RT-CODE(5).
RT-CODE(5)
3
Subscript or index, and the difference is real.
    subscript   an ordinary numeric field. The compiler multiplies it
                by the entry length on every reference.
    index       declared with INDEXED BY. Holds a byte displacement
                already, so no multiplication is needed.
4
Indexes are faster, and inside a loop over a large table that difference shows. They also cannot be used in arithmetic directly — SET RT-IDX UP BY 1, not ADD 1 TO RT-IDX.
SET RT-IDX UP BY 1ADD 1 TO RT-IDX
5
OCCURS DEPENDING ON makes the length variable.
    05  LINE-COUNT  PIC 9(3).
    05  ORDER-LINE  OCCURS 1 TO 200 TIMES
                    DEPENDING ON LINE-COUNT.
OCCURS DEPENDING ON
6
An order with three lines occupies three entries, not two hundred. The record's length varies with the data, which is how variable-length records work.
7
The trap is the order of operations. LINE-COUNT must hold the right value before the table is referenced or the record is written.
LINE-COUNT
8
    write with LINE-COUNT = 0   -> a record with no lines, silently
    read  with LINE-COUNT stale -> reading past what was populated
9
Nothing checks this. The field is just a number, and getting it wrong produces a malformed record rather than an error.
10
SEARCH and SEARCH ALL are the two lookups, and choosing wrongly is the classic question.
    SEARCH RATE-TABLE
        AT END      PERFORM NOT-FOUND
        WHEN RT-CODE(RT-IDX) = WS-WANTED
                    PERFORM FOUND
    END-SEARCH
SEARCHSEARCH ALL
11
That is SEARCH: linear, from the current index, works on any table in any order.
SEARCH
12
    SEARCH ALL RATE-TABLE
        AT END      PERFORM NOT-FOUND
        WHEN RT-CODE(RT-IDX) = WS-WANTED
                    PERFORM FOUND
    END-SEARCH
13
That is SEARCH ALL: a binary search. On a 1,000-entry table it takes about 10 comparisons instead of 500.
SEARCH ALL
14
SEARCH ALL has two requirements, and violating them does not raise an error:
SEARCH ALL
15
  - the table must be declared with `ASCENDING KEY IS` (or descending)
  - the data must actually be in that order
ASCENDING KEY IS
16
An unsorted table searched with SEARCH ALL returns "not found" for entries that are present. The program runs, and the results are wrong.
SEARCH ALL
17
Multi-dimensional tables nest OCCURS inside a group and are walked with nested PERFORM VARYING ... AFTER. Reference them with both subscripts: RATE(WS-I, WS-J).
OCCURSPERFORM VARYING ... AFTERRATE(WS-I, WS-J)
18
What the interviewer is probing.1. "What is the difference between an index and a subscript?" Probing: the distinction. Stalls: "Nothing." Moves up: a subscript is an ordinary numeric field holding an occurrence number; an index is a displacement maintained by the compiler and is faster.
19
2. "What does OCCURS DEPENDING ON give you?" Probing: the capability. Stalls: "A bigger table." Moves up: a variable-length table whose length comes from another field, which is how variable record formats are handled.
20
3. "What happens on a subscript out of range?" Probing: the hazard. Stalls: "An error message." Moves up: without bounds checking compiled in, it reads or writes adjacent storage silently — which is why SSRANGE matters in test builds.
21
4. "What is SEARCH ALL and what does it require?" Probing: the precondition. Stalls: "A sorted search." Moves up: a binary search, which requires the table to be sorted on the key and the ASCENDING KEY clause declared — an unsorted table gives wrong answers, not errors.