Understanding SEARCH and SEARCH ALL in COBOL

Understanding SEARCH and SEARCH ALL in COBOL: A Comprehensive Guide

Introduction: COBOL (Common Business-Oriented Language) is a programming language primarily used in the business sector for developing large-scale, data-processing applications. One of the essential features of COBOL is the ability to search for data within an array or table efficiently. In this blog post, we will explore two commonly used search techniques in COBOL: SEARCH and SEARCH ALL. We will delve into their syntax, differences, and provide relevant examples to help you understand how to leverage these powerful search functionalities in your COBOL programs.

 

Understanding SEARCH and SEARCH ALL in COBOL

  1. SEARCH: The SEARCH statement in COBOL is used to locate a specific value within a sorted array or table. It provides a straightforward and efficient way to perform searches based on a primary key or any other key field. The syntax for the SEARCH statement is as follows:
COBOL
SEARCH array/table
  AT END perform end-of-table
  WHEN condition perform found-condition
  NOT WHEN condition perform not-found-condition
END-SEARCH

Let’s break down the components of this syntax:

  • 'array/table‘: The name of the array or table where the search will be performed.
  • 'AT END': This condition is executed when the search value is greater than all the values in the array/table, indicating the end of the table has been reached.
  • WHEN condition‘: This condition is executed when the search value matches a value in the array/table.
  • NOT WHEN condition': This condition is executed when the search value does not match any value in the array/table.
  • END-SEARCH‘: Marks the end of the SEARCH block.

Example:

Consider a scenario where we have a sorted table 'EMPLOYEE-TABLE containing employee information such as employee ID ('EMP-ID) and employee name ('EMP-NAME). We want to search for a specific employee ID and perform different actions based on whether the ID is found or not.

COBOL
01 EMPLOYEE-TABLE.
   05 EMPLOYEE-ENTRY OCCURS 10 TIMES.
      10 EMP-ID PIC 9(4).
      10 EMP-NAME PIC X(20).
      
01 SEARCH-EMP-ID PIC 9(4).

PROCEDURE DIVISION.
  ...
  SEARCH EMPLOYEE-TABLE
    AT END DISPLAY "Employee not found."
    WHEN EMP-ID = SEARCH-EMP-ID
      DISPLAY "Employee found: " EMP-NAME
  END-SEARCH.
  ...

In this example, the SEARCH statement is used to search for a specific employee ID (SEARCH-EMP-ID') in the 'EMPLOYEE-TABLE. If the employee ID is found, it displays the corresponding employee name. Otherwise, it displays a “Employee not found” message.

SEARCH ALL:

While SEARCH is suitable for searching sorted arrays or tables, SEARCH ALL is used to search unsorted arrays or tables. It employs a binary search algorithm to efficiently locate a specific value. The syntax for the SEARCH ALL statement is as follows:

COBOL
SEARCH ALL array/table
  WHEN condition perform found-condition
  NOT WHEN condition perform not-found-condition
END-SEARCH

The key difference between SEARCH and SEARCH ALL is the absence of an AT END condition in SEARCH ALL. Since the array or table is unsorted, there is no way to determine the end of the table.

Example:

Let’s extend our previous example by using the SEARCH ALL statement to search for an employee ID in an unsorted EMPLOYEE-TABLE.

COBOL
01 EMPLOYEE-TABLE.
   05 EMPLOYEE-ENTRY OCCURS 10 TIMES.
      10 EMP-ID PIC 9(4).
      10 EMP-NAME PIC X(20).
      
01 SEARCH-EMP-ID PIC 9(4).

PROCEDURE DIVISION.
  ...
  SEARCH ALL EMPLOYEE-TABLE
    WHEN EMP-ID = SEARCH-EMP-ID
      DISPLAY "Employee found: " EMP-NAME
  END-SEARCH.
  ...

In this example, the SEARCH ALL statement is used to search for a specific employee ID (SEARCH-EMP-ID') in the unsorted EMPLOYEE-TABLE'. If the employee ID is found, it displays the corresponding employee name. Since the table is unsorted, there is no need for an 'AT END' condition.

Conclusion:

SEARCH and SEARCH ALL are powerful search functionalities in COBOL that allow developers to efficiently locate specific values within arrays or tables. By understanding the syntax and differences between these two statements, you can effectively implement search operations in your COBOL programs. Whether you’re working with sorted or unsorted data, COBOL’s search capabilities provide a valuable tool for processing business-oriented applications.

Remember to practice and experiment with different scenarios to gain a deeper understanding of SEARCH and SEARCH ALL in COBOL. By mastering these search techniques, you can enhance the performance and functionality of your COBOL programs while working with large datasets and complex business logic.