COBOL Search

Difference between Subscript and Index COBOL Search All

Advertisements


In COBOL, the SEARCH verb is used to search a table sequentially.

SEARCH is also called as linear or sequential search


Syntax:



Conditions:

  1. In the above syntax table name is the data item which is declared with OCCURS and INDEXED BY.
  2. Search uses to search both sorted/non sorted tables but index should be defined and used and searches sequentially.
  3. SEARCH is only applicable when the TABLE is searched with index.

Working process:

  1. The SEARCH searches a table sequentially starting at the data item which is pointed by the table index.
  2. The starting value of the table should be provided by the programmer.
  3. The VARYING is self explanatory that the index will vary and similarly the data items from the table also should vary based on the index.
  4. 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.
  5. If SEARCH WHEN executed one, then the SEARCH will be terminated and continue with the flow.

Note:

The programmer responsibility is to provide the valid value to the index which should be in the range.


Single dimensional array Practical Example - SEARCH:


Below example used to display the student passed or failed status by searching single dimensional array.


Code:


IDENTIFICATION DIVISION.                                        
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
 01 SECTION. 
   02 STUDENT. 
     03 SUBJECT PIC 9(3) OCCURS 6 TIMES INDEXED BY SEQ.
PROCEDURE DIVISION.                                             
    PERFORM SEQ FROM 1 BY 1 UNTIL SEQ > 6 
	ACCEPT MARKS[SEQ] 
    END-PERFORM. 
	   
    SET SEQ TO 1.
	   
    SEARCH STUDENT VARYING SEQ
	AT END DISPLAY ‘ STUDENT PASSED’ 
	WHEN SUBJECT[SEQ] < 35 
	     DISPLAY ‘STUDENT FAILED’ 
    END-SEARCH. 
							       
    STOP RUN.                             


Two dimensional array Practical Example - SEARCH:


Below example used to display passed or failed status for two students by searching two dimensional array.


Code:


IDENTIFICATION DIVISION.                                        
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
 01 SECTION 
  02 STUDENT OCCURS 2 TIMES INDEXED BY S. 
    03 SUBJECT PIC 9(3) OCCURS 6 TIMES.INDEXED BY M.
PROCEDURE DIVISION.                                             
                                 
   PERFORM S FROM 1 BY1 UNTIL S >2 
   PERFORM M FROM 1 BY 1 UNTIL M > 6 
      ACCEPT MARKS[S,I] 
   END-PERFORM 
   END-PERFORM.

   PERFORM S FROM 1 BY1 UNTIL S >2 
   SET I TO 1. 
   SEARCH STUDENT 
       AT END DISPLAY ‘STUDENT‘ S ‘ PASSED’ 
   WHEN SUBJECT[S,I] < 35 
        DISPLAY ‘STUDENT NO ‘ S ‘ FAILED’ 
   END-SEARCH 
   END-PERFORM. 
	   
   STOP RUN.                   



Difference between Subscript and Index COBOL Search All

Advertisements