Friday, 12 December 2008

UNITE-5::SEARCH VERB:

The SEARCH verb is used to locate elements in one-dimensional tables.

Syntax:

RULES FOR THE SEARCH VERB:

• The SEARCH verb can only be applied to a table which has the OCCURS clause and INDEXED BY phrase.
• Before the use of the SEARCH verb, the index must have some initial value. The initial value must not exceed the size of the table. If it exceeds the search is terminated immediately. Then if the AT END clause is specified, statements after AT END will be executed, otherwise the control passes to the next sentence.
• If the AT END condition is specified, as in the case of the first example, and if the element which is being searched is not found in the table, the statements after the AT END clause will be executed and the control will then be transferred to the next sentence if the statements after AT END do not transfer the control elsewhere in the program.
• The SEARCH verb starts with the initial value of the index and tests whether the conditions stated in the WHEN clauses have been satisfied or not. If none of the conditions are satisfied the index is incremented automatically by 1. The process is continued until the index value exceeds the size of the table and the search is terminated.
• Connected with the VARYING option, identifier-2 can be either a data, an integral elementary item or an index data item (described with USAGE AS INDEX clause). The purpose of specifying the VARYING clause is that identifier-2 is also incremented each time the index of the table is incremented.
• The following is a flow chart showing the actions taken during the execution of a SEARCH statement with VARYING option and two WHEN clauses.

Flow Chart:


EXAMPLE:
Let us consider the following problem. Suppose each element of a table consist of three fields, namely, the account number of a person, the name of that person and the amount that he has deposited. There are four hundred such elements in the table and we want to find out whether a particular name is present in the table or not. The desired name is given in the field called NAME and if this name appears in an element of the table, we would like to display the name as well as the corresponding account number and amount.

The DATA DIVISION entries for this problem are as follows:
DATA DIVISION.
.
.
.
77 NAME PIC X(20).
01 SAVINGS-BANK-ACCOUNT.
02 ACCOUNT-TABLE OCCURS 400 TIMES INDEXED BY A1.
03 ACCOUNT-NUMBER PIC 9(6).
03 AMOUNT PIC 9(6).99.

The following PROCEDURE DIVISION statement can be a solution to the above problem.
PROCEDURE DIVISION.
.
.
.
SET A1 TO 1.
SEARCH ACCOUNT-TABLE.
AT END DISPLY “NAME NOT FOUND”
WHEN NAME=NAME-OF-THE-PERSON(A1)
DISPLAY ACCOUNT-NUMBER 9A1),NAME, AMOUNT(A1).


In the above SEARCH statement, there are two parts-the AT END part and the WHEN part. If the condition NAME=NAME-OF-THE-PERSON(A1) is satisfied for some value of the index name A1, the statement DISPLAY ACCOUNT-NUMBER(A1), NAME, AMOUNT(A1) is executed. The AT END part is executed and the condition is not satisfied for any value of A1. The increment of A1 is taken care of by the SEARCH verb.
Binary Search

The above Search verb is work as Binary search method. In this case the total number of comparison is less than that of linear search.
It may be noted here that when this type of search verb is used, the SET verb is not needed to initialize the index.

No comments:

Post a Comment