COBOL SearchDifference between Subscript and Index COBOL Search All
In COBOL, the SEARCH verb is used to search a table sequentially.
SEARCH is also called as linear or sequential search
- In the above syntax table name is the data item which is declared with OCCURS and INDEXED BY.
- Search uses to search both sorted/non sorted tables but index should be defined and used and searches sequentially.
- SEARCH is only applicable when the TABLE is searched with index.
- The SEARCH searches a table sequentially starting at the data item which is pointed by the table index.
- The starting value of the table should be provided by the programmer.
- 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.
- 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.
- If SEARCH WHEN executed one, then the SEARCH will be terminated and continue with the flow.
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.
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.
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