COBOL Search All

COBOL Search Difference between Search and Search all

Advertisements


SEARCH ALL is used to search a table by using Binary search.

To proceed with the SEARCH ALL, the table used for to search is should be in sorted order.

If the table is not sorted order, the SEARCH ALL will not work effectively.

SEARCHALL is also called as binary search.


Syntax:



Conditions:

1. In the above syntax table name is the data item which is declared with OCCURS and INDEXED BY.

2. SEARCH ALL uses to search only sorted tables but index should be defined and searches in binary mode.

3. SEARCH ALL is only applicable when the TABLE is searched with index.


Working process:

1. The SEARCH ALL searches a table in binary mode.

2. The starting value of the table should not required to provided by the programmer and it will control automatically.

3. The AT END is very similar to the AT END concept in Files. But here the AT END will trigger only when the INDEX reached the OCCURS maximum value.

4. If SEARCHALL WHEN executed one, then the SEARCH ALL will be terminated and continue with the flow.


Example:


Below example used to search student name from pre-sorted array.


Code:


 IDENTIFICATION DIVISION.                                        
 PROGRAM-ID. PERFTIMI.                                           
 ENVIRONMENT DIVISION.                                           
 DATA DIVISION.                                                  
 WORKING-STORAGE SECTION.                                        
  01 SECTION 
     02 STUDENT-NAME OCCURS PIC X(30) 10 TIMES
              ASCENDING KEY IS STUDENT-NAME
              INDEXED BY STD-IDX. 
  01 I               PIC X(02).
  01 IN-STD-NAME     PIC X(30) VALUE SPACES.

 PROCEDURE DIVISION.                                             
     
	ACCEPT IN-STD-NAME
	
	PERFORM I FROM 1 BY 1 UNTIL I > 10 
	   ACCEPT STUDENT-NAME[I]  
	END-PERFORM.
	
	SET STD-IDX TO 1.
	
	SEARCH ALL STUDENT-NAME 
	       AT END DISPLAY ‘STUDENT DETAILS NOT FOUND' 
	 WHEN STUDENT-NAME (STD-IDX) = IN-STD-NAME 
	      DISPLAY ‘STUDENT DETAILS FOUND'
	END-SEARCH. 
	
	STOP RUN.                                          



COBOL Search Difference between Search and Search all

Advertisements