Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL Tables and Arrays

Intermediate 🕑 10 min read 👁 0 views

18

Code Example


## COBOL Tables (Arrays)

COBOL tables store multiple occurrences of data items. They're defined using OCCURS clause and accessed via subscripts or indexes.

### OCCURS Clause Syntax

\`\`\`cobol
level-number data-name OCCURS integer TIMES
    [INDEXED BY index-name]
    [ASCENDING/DESCENDING KEY IS key-name].
\`\`\`

### Types of Tables

1. **Single-dimension**: Simple array
2. **Multi-dimension**: Nested OCCURS (up to 7 levels)
3. **Variable-length**: OCCURS DEPENDING ON

### Subscripts vs Indexes

**Subscripts:**
- Integer or data-name
- Range: 1 to max occurrences
- Converted to displacement at runtime

**Indexes:**
- Defined with INDEXED BY
- Manipulated with SET, SEARCH
- More efficient (pre-calculated displacement)

### SEARCH Statement

- **SEARCH**: Sequential search
- **SEARCH ALL**: Binary search (requires sorted table with KEY)

### Performance Tips

- Use indexes for frequently accessed tables
- SEARCH ALL is faster for large sorted tables
- Consider table size in WORKING-STORAGE