RAMESH KRISHNA REDDY
 Online Tutorials | PREV | TOP | NEXT

DRONA SERIES
COBOL STUDY MATERIAL

SEARCH ALL

Author : Vasanta T.

 SEARCH ALL SEARCH ALL Search all is similar to binary search. Binary search is a way of searching for element [array] within group of elements. Binary search works by comparing an input value to the middle element of the array. Elements within array must be sorted in either ascending or descending order before binary search and Binary search trying to match search element with the middle element. If the search element is equals to middle element, search process end there. If the search element is greater than middle element then array is divided into 2 equal parts and search will happen in second half. If element is less than middle element then it again search within first half. Whether it selected first half or second half, it again select the middle element in that part and continue this process until either element is found or there are no elements to search. Binary Search Algorithm / half-interval search If table is contain N even number of elements then middle number is identified as N/2. If that N number is odd then middle number is identified as (N+1)/2. If we are trying to find an element “14” within set of numbers then the following process happens to find element. 18, 14, 1, 6, 9, 8, 3 Step 1: All elements must be in sorted order. 1, 3, 6, 8, 9, 14, 18 TIP : SEARCH ALL dont do this. we need to use ascending keyword while defining array using OCCURS clause. Step 2: Find middle element. In this group we have 7 elements. accoridng to binary search rules we need to calcualte middle element by using formula N-1/2. So, in our array, middle element is 8. Step 3: Binary search match 14 with middle element 8 and finds 14 > 8 so divides this group into 2 equal groups Group 1: 1, 3, 6 Groups 2: 9, 14, 18 Binary search compares second half middle element 14 with 14. Search found the element. Search all stops the search here. When the Table entries are arranged in sequence by some field, such as EMP-ID, the most efficient type of lookup is a binary search. The SEARCH ALL statement provides a means for doing the same. TIP : If array is large (100+) it is good practice to go for search all. If array is small then go for search. Syntax: SEARCH ALL Table–name [AT END imperative statements WHEN data name 1 IS EQUAL TO [identifier 2 or literal 1 or arithmetic expression 1] [AND data name 2 IS EQUAL TO [identifier 2 or literal 1 or arithmetic expression 1] IMPERATIVE STATEMENT [END SEARCH] 1. The condition following the word WHEN can only test for equality. 2. The condition has to be equality. 3. Only one WHEN clause can be used. 4. Table must be sorted order. 5. The OCCURS item and its index, which define the Table argument, must appear to the left of the equal to sign. 6. Does not require SET statement prior to SEARCH ALL. SERACH ALL Example : 01 WT-FIELDS. 05 WT-CODE-CTRL OCCURS 100 TIMES INDEXED BY WT-CODE-CTRL-INDEX ASCENDING KEY IS WT-CODE-VALUE 10 WT-CODE-VALUE PIC X(03). 10 WT-CODE-DESC PIC X(03). SEARCH ALL WT-CODE-CTRL AT END DISPLAY “SEARCH ELEMENET NOT FOUND IN TABLE WT-CODE-CTRL” WHEN WT-BILLER-CODE (WT-CODE-CTRL-INDEX) = “121” DISPLAY WT-CODE-DESC (WT-CODE-CTRL-INDEX) END-SEARCH.

NEXT CHAPTER TOPIC : COBOL

Visit COBOL books section in this site for good books