MainframeMaster

COBOL Table Handling Concepts

Tables (arrays) in COBOL are declared with OCCURS and accessed by subscript or index. Use INDEXED BY with SEARCH and SEARCH ALL to traverse efficiently and safely.

Declare a Table

cobol
1
2
3
4
5
6
7
8
9
WORKING-STORAGE SECTION. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 1 TO 500 TIMES DEPENDING ON CUSTOMER-COUNT INDEXED BY CUST-IX. 10 CUST-ID PIC 9(6). 10 CUST-NAME PIC X(30). 10 CUST-BAL PIC 9(9)V99. 01 CUSTOMER-COUNT PIC 9(3) VALUE 0.

- OCCURS: defines array size (1 to 500 entries). DEPENDING ON: allows dynamic sizing at runtime based on CUSTOMER-COUNT. INDEXED BY: creates CUST-IX as a special index variable for SEARCH operations. Each entry contains customer ID, name, and balance.

Fixed vs Variable Size Tables

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Fixed size table 01 FIXED-TABLE. 05 FIXED-ENTRY OCCURS 100 TIMES INDEXED BY FIX-IX. 10 FIX-ID PIC 9(4). 10 FIX-NAME PIC X(20). *> Variable size table 01 VAR-TABLE. 05 VAR-ENTRY OCCURS 1 TO 1000 TIMES DEPENDING ON VAR-COUNT INDEXED BY VAR-IX. 10 VAR-ID PIC 9(4). 10 VAR-NAME PIC X(20). 01 VAR-COUNT PIC 9(4) VALUE 0.

Fixed tables always allocate full space. Variable tables only use space up to the DEPENDING ON value, saving memory when you have fewer entries than the maximum.

Sequential Scan (Subscript)

cobol
1
2
3
4
5
01 I PIC 9(3). ... PERFORM VARYING I FROM 1 BY 1 UNTIL I > CUSTOMER-COUNT DISPLAY CUST-ID(I) CUST-NAME(I) END-PERFORM.

Subscripts are numeric counters that must stay within bounds (1 to CUSTOMER-COUNT). Use PERFORM VARYING for safe iteration. Always check bounds before accessing table elements to avoid subscript errors.

Loading Data into Tables

cobol
1
2
3
4
5
6
7
8
9
10
11
12
MOVE 0 TO CUSTOMER-COUNT OPEN INPUT CUSTOMER-FILE PERFORM UNTIL EOF OR CUSTOMER-COUNT = 500 READ CUSTOMER-FILE AT END SET EOF TO TRUE NOT AT END ADD 1 TO CUSTOMER-COUNT MOVE CUST-ID-IN TO CUST-ID(CUSTOMER-COUNT) MOVE CUST-NAME-IN TO CUST-NAME(CUSTOMER-COUNT) MOVE CUST-BAL-IN TO CUST-BAL(CUSTOMER-COUNT) END-READ END-PERFORM CLOSE CUSTOMER-FILE.

Load tables by reading records and incrementing the count. Always check for EOF and maximum capacity to prevent overruns. The count tells you how many valid entries you have.

SEARCH (Linear) with INDEXED BY

cobol
1
2
3
4
5
6
SET CUST-IX TO 1 SEARCH CUSTOMER-ENTRY AT END DISPLAY 'Not found' WHEN CUST-ID(CUST-IX) = 123456 DISPLAY 'Found: ' CUST-NAME(CUST-IX) END-SEARCH.

SET index TO 1 initializes the index to start searching from the first entry. WHEN condition runs when a match is found; otherwise SEARCH automatically advances the index to the next entry. AT END executes if no match is found after checking all entries.

Multiple Search Conditions

cobol
1
2
3
4
5
6
7
8
SET CUST-IX TO 1 SEARCH CUSTOMER-ENTRY AT END DISPLAY 'No match found' WHEN CUST-ID(CUST-IX) = SEARCH-ID DISPLAY 'Found by ID: ' CUST-NAME(CUST-IX) WHEN CUST-NAME(CUST-IX) = SEARCH-NAME DISPLAY 'Found by name: ' CUST-ID(CUST-IX) END-SEARCH.

You can have multiple WHEN conditions. The first one that matches will execute, then SEARCH stops. Use this for finding records by different criteria.

SEARCH ALL (Binary Search)

cobol
1
2
3
4
5
6
*> Table must be sorted ascending by CUST-ID first SEARCH ALL CUSTOMER-ENTRY AT END DISPLAY 'Not found' WHEN CUST-ID(CUST-IX) = 123456 DISPLAY 'Found fast: ' CUST-NAME(CUST-IX) END-SEARCH.

SEARCH ALL requires the table to be sorted in ascending order by the search key. It uses binary search algorithm, checking the middle element first, then eliminating half the remaining elements with each comparison. Extremely fast for large tables (logarithmic time complexity).

Sorting Tables Before SEARCH ALL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Simple bubble sort (for small tables) PERFORM VARYING I FROM 1 BY 1 UNTIL I > CUSTOMER-COUNT - 1 PERFORM VARYING J FROM 1 BY 1 UNTIL J > CUSTOMER-COUNT - I IF CUST-ID(J) > CUST-ID(J + 1) MOVE CUST-ID(J) TO TEMP-ID MOVE CUST-ID(J + 1) TO CUST-ID(J) MOVE TEMP-ID TO CUST-ID(J + 1) MOVE CUST-NAME(J) TO TEMP-NAME MOVE CUST-NAME(J + 1) TO CUST-NAME(J) MOVE TEMP-NAME TO CUST-NAME(J + 1) END-IF END-PERFORM END-PERFORM.

Sort all related fields together when swapping. For larger tables, consider external SORT utility or load from pre-sorted files for better performance.