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

COBOL Tables and Arrays

Intermediate 🕑 25 min read 👁 0 views

Tables in COBOL

Tables (arrays) in COBOL are defined using the OCCURS clause. They allow you to store multiple occurrences of data items.

Defining Tables

Use OCCURS clause to define the number of elements:

  • Fixed-size tables - OCCURS n TIMES
  • Variable-size tables - OCCURS n TO m TIMES DEPENDING ON

Subscripts vs Indexes

SubscriptIndex
Numeric data itemDefined with INDEXED BY
Starts from 1Uses displacement
Slower performanceFaster performance
More flexibleUsed with SEARCH

Table Operations

  • SEARCH - Sequential search
  • SEARCH ALL - Binary search (requires ASCENDING/DESCENDING KEY)
  • SET - Manipulate index values

Multi-dimensional Tables

COBOL supports tables with multiple dimensions using nested OCCURS clauses.

Best Practices

  • Use INDEXED BY for better performance
  • Initialize tables before use
  • Check bounds to prevent S0C4 abends
  • Use SEARCH ALL for large sorted tables

Code Example

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       * Simple table
       01 WS-MONTH-TABLE.
          05 WS-MONTH-NAME PIC X(10) OCCURS 12 TIMES.

       * Table with index
       01 WS-EMPLOYEE-TABLE.
          05 WS-EMP-ENTRY OCCURS 100 TIMES
             INDEXED BY WS-EMP-IDX.
             10 WS-EMP-ID    PIC 9(6).
             10 WS-EMP-NAME  PIC X(30).
             10 WS-EMP-DEPT  PIC X(10).

       * Table for SEARCH ALL (must be sorted)
       01 WS-PRODUCT-TABLE.
          05 WS-PROD-COUNT PIC 9(3).
          05 WS-PROD-ENTRY OCCURS 1 TO 500 TIMES
             DEPENDING ON WS-PROD-COUNT
             ASCENDING KEY IS WS-PROD-CODE
             INDEXED BY WS-PROD-IDX.
             10 WS-PROD-CODE  PIC X(10).
             10 WS-PROD-DESC  PIC X(30).
             10 WS-PROD-PRICE PIC 9(5)V99.

       * Two-dimensional table
       01 WS-SALES-TABLE.
          05 WS-REGION OCCURS 4 TIMES INDEXED BY REG-IDX.
             10 WS-QUARTER OCCURS 4 TIMES INDEXED BY QTR-IDX.
                15 WS-SALES-AMT PIC 9(9)V99.

       PROCEDURE DIVISION.
       TABLE-EXAMPLES.

       * Initialize table
           INITIALIZE WS-EMPLOYEE-TABLE.

       * Using subscript
           MOVE "JANUARY" TO WS-MONTH-NAME(1).
           MOVE "FEBRUARY" TO WS-MONTH-NAME(2).

       * Using index - SET
           SET WS-EMP-IDX TO 1.
           MOVE "123456" TO WS-EMP-ID(WS-EMP-IDX).

       * Sequential SEARCH
           SET WS-EMP-IDX TO 1.
           SEARCH WS-EMP-ENTRY
               AT END
                   DISPLAY "EMPLOYEE NOT FOUND"
               WHEN WS-EMP-ID(WS-EMP-IDX) = "123456"
                   DISPLAY "FOUND: " WS-EMP-NAME(WS-EMP-IDX)
           END-SEARCH.

       * Binary SEARCH ALL
           SEARCH ALL WS-PROD-ENTRY
               AT END
                   DISPLAY "PRODUCT NOT FOUND"
               WHEN WS-PROD-CODE(WS-PROD-IDX) = "PROD001"
                   DISPLAY "PRICE: " WS-PROD-PRICE(WS-PROD-IDX)
           END-SEARCH.

       * Accessing 2D table
           MOVE 50000.00 TO WS-SALES-AMT(1, 1).
           SET REG-IDX TO 2.
           SET QTR-IDX TO 3.
           ADD 1000 TO WS-SALES-AMT(REG-IDX, QTR-IDX).

           STOP RUN.