Write a COBOL logic using SEARCH ALL for a binary search on a sorted table.
- User
Write a COBOL logic using
SEARCH ALLfor a binary search on a sorted table. Assistant
Write a COBOL logic using SEARCH ALL for a binary search on a sorted table.
1. Understand the Goal: The user wants COBOL code demonstrating the SEARCH ALL statement. This implies a binary search on a sorted table (array).
Identify Key COBOL Concepts:
OCCURS clause. Crucially, for SEARCH ALL, the data must be sorted, and the keys must be defined using the KEY IS clause in the DATA DIVISION.SEARCH ALL requires an index (INDEXED BY). This is usually handled implicitly by the compiler during the search, but the declaration is mandatory.SEARCH ALL Statement: This is the core logic. It requires:
AT END clause (what happens if not found).WHEN condition (checking the key against a search argument).Construct the Example Scenario:
PRODUCT-TABLE.PRODUCT-ID (Key), PRODUCT-NAME, PRODUCT-PRICE.Drafting the Data Division:
WS-SEARCH-ID.WS-FOUND-FLAG (Y/N).01 PRODUCT-TABLE.
05 PRODUCT-ENTRY OCCURS 1000 TIMES
ASCENDING KEY IS PRODUCT-ID <-- Critical for Binary Search
INDEXED BY WS-TAB-IDX. <-- Critical for Search
10 PRODUCT-ID PIC 9(05).
10 PRODUCT-NAME PIC X(20).
10 PRODUCT-PRICE PIC 9(05)V99.
Drafting the Procedure Division:
SEARCH ALL PRODUCT-ENTRY
AT END
DISPLAY "Product not found"
WHEN PRODUCT-ID (WS-TAB-IDX) = WS-SEARCH-ID
DISPLAY "Found: " PRODUCT-NAME (WS-TAB-IDX)
SET WS-FOUND-FLAG TO 'Y'
END-SEARCH.
Refining the Logic (Best Practices & Nuances):
SEARCH ALL does not require initializing the index (setting it to 1) like a serial search does. The compiler handles the midpoint calculations. I should mention this as a note.ASCENDING KEY or DESCENDING KEY in the OCCURS clause is non-negotiable for SEARCH ALL.SEARCH ALL is almost exclusively used for equality (=).INDEXED BY clause inside the WHEN condition (e.g., PRODUCT-ID(WS-TAB-IDX)).Structuring the Final Output:
SEARCH ALL is a binary search and requires a sorted table.ASCENDING/DESCENDING KEY, INDEXED BY).Self-Correction/Review during drafting:
KEY clause. The code example won't physically sort it (that's a SORT verb task), but I will assume the table is pre-populated with sorted data for the sake of the example.Final Polish: Write the code clearly with comments. (Proceed to generate output based on these steps).
Here is the COBOL logic for performing a binary search using the SEARCH ALL statement.
For SEARCH ALL to work correctly, two conditions must be met in the Data Division:
INDEXED BY phrase.KEY IS phrase (indicating the sort order). IDENTIFICATION DIVISION.
PROGRAM-ID. BINARY-SEARCH-EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
* Define the search input
01 WS-SEARCH-ID PIC 9(04) VALUE 1050.
* Define a flag to handle search results
01 WS-FOUND-FLAG PIC X(01) VALUE 'N'.
88 FOUND VALUE 'Y'.
88 NOT-FOUND VALUE 'N'.
* Define the Sorted Table (Array)
01 PRODUCT-TABLE.
05 PRODUCT-ENTRY OCCURS 10 TIMES
ASCENDING KEY IS WS-PROD-ID <-- CRITICAL: Defines sort order
INDEXED BY WS-TAB-IDX. <-- CRITICAL: Defines the index
10 WS-PROD-ID PIC 9(04).
10 WS-PROD-NAME PIC X(20).
10 WS-PROD-PRICE PIC 9(05)V99.
PROCEDURE DIVISION.
0000-MAIN.
* (Assumption: The table is pre-populated with sorted data)
* For this example, we perform the search immediately.
PERFORM 1000-DO-SEARCH
IF FOUND
DISPLAY "Product Found: " WS-PROD-NAME(WS-TAB-IDX)
DISPLAY "Price: " WS-PROD-PRICE(WS-TAB-IDX)
ELSE
DISPLAY "Product ID " WS-SEARCH-ID " not found."
END-IF.
STOP RUN.
1000-DO-SEARCH.
* It is GOOD PRACTICE to set the index to 1 before searching,
* though SEARCH ALL manages the index pointer internally.
SET WS-TAB-IDX TO 1.
* The Binary Search Logic
SEARCH ALL PRODUCT-ENTRY
AT END
* This block executes if the key is not in the table
SET NOT-FOUND TO TRUE
WHEN WS-PROD-ID(WS-TAB-IDX) = WS-SEARCH-ID
* This block executes if a match is found
* WS-TAB-IDX now holds the position of the found item
SET FOUND TO TRUE
DISPLAY "Match found at index: " WS-TAB-IDX
END-SEARCH.
ASCENDING KEY IS WS-PROD-ID: This tells the COBOL compiler that the data inside the table is sorted by WS-PROD-ID. SEARCH ALL relies on this fact to split the table in half repeatedly (Binary Search). If the data is not actually sorted, the search results will be unpredictable.INDEXED BY WS-TAB-IDX: You must use a specific index (defined without a PIC clause) for the table. You cannot use standard subscripts (like WS-COUNT) for a binary search.SEARCH ALL StatementSEARCH ALL: This invokes the binary search algorithm.AT END: This logic block executes only if the entire table (or the relevant sorted partitions) has been scanned and no match was found.WHEN ... = ...: This is the condition. You must compare the table's key field (subscripted by the index) to your search value using the equals (=) operator. You cannot use >, <, or other logical operators in a SEARCH ALL condition.If the item is found, the index (WS-TAB-IDX) is automatically updated to point to that specific row. You can then use that index to access other fields in that row (e.g., WS-PROD-NAME(WS-TAB-IDX)).