OCCURS
The COBOL clause that defines a table: a field repeated a set number of times, like an array in other languages.
Also written COBOL table, COBOL array, OCCURS DEPENDING ON, subscript, INDEXED BY, SEARCH
OCCURS defines a table, what other languages call an array. Writing 05 MONTH-TOTAL PIC 9(7) OCCURS 12 TIMES gives twelve fields with the same name and format, one for each month.
You refer to one entry with a subscript in brackets: MONTH-TOTAL(3) is the third. Subscripts start at 1, not 0. A table can also be given an index with INDEXED BY, which is moved with SET instead of MOVE and is used by the SEARCH statement to look through the table.
OCCURS DEPENDING ON makes the number of entries depend on another field, for records or tables whose length changes. Tables are usually filled and processed with PERFORM VARYING, which steps a subscript from 1 up to the number of entries.
By default, COBOL does not check that a subscript is in range. Using MONTH-TOTAL(13) on a twelve-entry table quietly reads or changes whatever memory comes after the table. The SSRANGE compile option adds checking, and many sites use it in testing.
Where can I learn this hands-on?
Right here, at ZedBank. You take a junior job at a simulated bank and learn each part of the work by doing it, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- DATA DIVISIONThe part of a COBOL program that declares every record layout and variable it will use.
- Level numberThe two-digit number in front of a COBOL data item that shows how fields nest inside a record.
- WORKING-STORAGEThe section of a COBOL program that declares its internal variables: counters, totals, flags and work areas.
- PERFORMThe COBOL statement that runs a paragraph and returns, and the way loops are written.
- PICTURE clauseThe COBOL declaration that says what kind of data a field holds and how long it is, written as PIC.