SEARCH ALL |
In today's tutorial, you will learn how to search the array/tables in COBOL. This tutorial examines the operation of the SEARCH and SEARCH ALL verbs. The term SEARCH in COBOL is used for linear searches, and SEARCH ALL in COBOL is used for binary searches. Also, you will learn mandatory entries for Data Division and Procedure Division.
Tables are extremely important in COBOL, as they are in any other computer language. You can refer Master ARRAY In COBOL In Just A Few Minutes! if you are not familiar with declaring an array in COBOL. So, let's start with the tutorial.
What is a search in COBOL?
In COBOL, or in any other programming language the term search is used to find specific value in an array/table. The task of searching a table/array to determine whether it contains a particular value is a common operation. The method used to search a table depends heavily on the way the values are organized in the table/array.
What is a search in COBOL?
In COBOL, or in any other programming language the term search is used to find specific value in an array/table. The task of searching a table/array to determine whether it contains a particular value is a common operation. The method used to search a table depends heavily on the way the values are organized in the table/array.
What is a linear search in COBOL?
In COBOL, If you define a one dimension array and values are not ordered, then the only strategy available is a linear search. A linear search starts at the first element and then examines each succeeding element until the item is found or until the end of the table is reached (an item not found).
Linear Search Syntax.
SEARCH identifier [VARYING {identifier-2/index-name}]
[AT END statement]
[WHEN condition statements]
[END-SEARCH]
Note: The SEARCH statement does not initialize the search index. You must do this with a SET statement (described earlier in the chapter) or by using some other programmatic way to ensure that it has a valid starting value before you start the search.
What is a binary search in COBOL?
In COBOL, A binary search works by dividing the table in half and determining whether the item sought is in the top half of the table or the bottom half. This process continues until the item is found or it is determined that the item is not in the table.
COBOL has special verbs that let you search for tables using either strategy. The SEARCH verb is used for linear searches, and the SEARCH ALL verb is used for binary searches.
SEARCH and SEARCH ALL in COBOL with example.
SEARCH ALL Syntax.
SEARCH ALL identifier-1
AT END imperative statement-1
WHEN equal-condition-1 and equal-condition-2
{imperative statement-2}
{NEXT SENTENCE}
END-SEARCH
Note: In COBOL, If the values are ordered, then you have the option of using either a linear search or a binary search.
COBOL SEARCH VS COBOL SEARCH ALL
One advantage of using SEARCH or SEARCH ALL rather than a handcrafted search is that because these are specialized instructions, their operation can be optimized. Part of that optimization involves creating a special subscript to be used when searching the table. You create this special subscript using an extension to the OCCURS clause called the INDEXED BY clause.
INDEXED BY Clause
Before you can use SEARCH or SEARCH ALL to search a table, you must define the table as having an index item associated with it. Using an index makes searching more efficient. Because the index is linked to a particular table, the compiler—taking into account the size of the table—can choose the most efficient representation possible for the index. This speeds up the search.
Using SET to Manipulate the Table Index
A table index is a special data item. It has no PICTURE clause, it is associated with a particular table, and the compiler defines the index using the most computationally efficient representation possible. Because of its special binary representation, the table index cannot be displayed and can only be assigned a value, or have its value assigned, by the SET verb. Similarly, the SET verb must be used to increment or decrement the value of an index item.
SEARCH and SEARCH ALL in COBOL with example.
SEQUENTIAL SEARCH in COBOL example
Here is an example of using this format of the SEARCH statement:
SET PRICE-TABLE-INDEX TO 1.
MOVE "N" TO PTABLE-EOF-SWITCH.
PERFORM UNTIL PTABLE-EOF
SEARCH PRICE-GROUP
AT END
MOVE "Y" TO PTABLE-EOF-SWITCH
WHEN ITEM-PRICE (PRICE-TABLE-INDEX) = ITEM-PRICE
DISPLAY "ITEM NUMBER: " ITEM-NUMBER (PRICE-TABLE-INDEX)
END-SEARCH
SET PRICE-TABLE-INDEX UP BY 1
END-PERFORM.
SEARCH ALL IN COBOL with example.
Here is an example of using this format of the SEARCH statement:
01 STATE-SALES-TAX.
03 TAX-TABLE OCCURS 50 TIMES INDEXED BY STATE-INDEX
ASCENDING KEY IS STATE.
05 STATE PIC XX.
05 TAX PIC 99V999.
01 RECORD-X.
03 NAME PIC X(20)
…
03 STATE PIC XX.
SEARCH ALL TAX-TABLE
AT END
DISPLAY “No Such State”
GO TO BAD-STATE
WHEN STATE OF TAX-TABLE(STATE-INDEX) = STATE OF RECORD-X
CONTINUE
END-SEARCH.
* At this point, STATE-INDEX points to the table entry for this
* state
MOVE TAX(STATE-INDEX) TO …
This comment has been removed by a blog administrator.
ReplyDelete