Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Debugging
Debugging | Intermediate | COBOL

Fix Table Search

Problem Description

SEARCH never finds existing item:
SET IDX TO 1.
SEARCH WS-TABLE AT END DISPLAY "NOT FOUND".

Expected Output

Should find matching element

Hints

SEARCH requires SET index before searching.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. FIXSRCH. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-ITEM OCCURS 5 TIMES INDEXED BY IDX. 10 WS-CODE PIC X(3). 10 WS-DESC PIC X(10). 01 WS-SEARCH PIC X(3) VALUE "B02". PROCEDURE DIVISION. MOVE "A01" TO WS-CODE(1). MOVE "B02" TO WS-CODE(2). MOVE "C03" TO WS-CODE(3). * BUG: Missing WHEN condition in SEARCH * FIX: Add WHEN clause to specify match condition SET IDX TO 1. SEARCH WS-ITEM AT END DISPLAY "NOT FOUND" WHEN WS-CODE(IDX) = WS-SEARCH DISPLAY "FOUND AT POSITION " IDX END-SEARCH. STOP RUN.

Explanation:

SEARCH needs WHEN clause to specify the condition to match.