📜  COBOL 中搜索和全部搜索的区别

📅  最后修改于: 2022-05-13 01:55:18.691000             🧑  作者: Mango

COBOL 中搜索和全部搜索的区别

在 COBOL 中有两种方法可以执行搜索操作,首先使用传统方法,即在 PERFORM 语句的帮助下应用循环,或者使用预定义的动词:SEARCH 和 SEARCH ALL。 SEARCH 和 SEARCH ALL 动词只能在 INDEXED FILES 中使用,因为它们使用与文件关联的索引变量。

SEARCH VERB 在记录/数组中执行线性搜索,而 SEARCH ALL VERB 在记录/数组中执行二分搜索。

搜索

COBOL 中的搜索动词可用于执行线性搜索,使用表名作为索引。

句法:

SEARCH {TableName} [VARYING {IndexName}]             
                [AT END Code1]     
       {WHEN condition {Code2}                     
                       {NEXT SENTENCE    }
[END-SEARCH] 

例子:



WORKING-STORAGE SECTION.
           77 N PIC 99.
           77 SEARCHRNO PIC 99.
           01 ARRAY.
              02 ARR OCCURS 10 TIMES ASCENDING KEY IS RNO INDEXED BY I.
                03 RNO PIC 99.
        PROCEDURE DIVISION.  
            //AFTER ENTERING THE ELEMENTS IN ARRAY 
            //WE SEARCH THE ROLL NUMBER ENTERED 
            //BY USER IN VARIABLE SEARCHRNO.     
           SET I TO 1.
           SEARCH ARR AT END DISPLAY "NOT FOUND"
           WHEN RNO(I) = SEARCHRNO DISPLAY "FOUND ROLL NUMBER".
           STOP RUN.

输出1:搜索实际属于数组的元素。

输出 2:搜索不属于数组的元素。

搜索全部

COBOL 中的 Search All 动词用于使用索引(表名)在 COBOL 中执行二进制搜索。ntax

句法:

SEARCH ALL {TableName} [VARYING {IndexName}]             
         [AT END Code1]     
{WHEN condition {Code2}                     
                {NEXT SENTENCE    }
[END-SEARCH] 

例子:

WORKING-STORAGE SECTION.
           77 SEARCHRNO PIC 99.
           77 N PIC 99.
           01 ARRAY.
              02 ARR OCCURS 10 TIMES ASCENDING KEY IS RNO INDEXED BY I.
                03 RNO PIC 99.
       PROCEDURE DIVISION.
           //AFTER ENTERING THE ELEMENTS IN ARRAY 
            //WE SEARCH THE ROLL NUMBER ENTERED 
            //BY USER IN VARIABLE SEARCHRNO.     
           SEARCH ALL ARR AT END DISPLAY "NOT FOUND ROLL NUMBER"
           WHEN RNO(I) = SEARCHRNO DISPLAY "FOUND ROLL NUMBER".
           STOP RUN.
           

输出: 1:搜索实际属于数组的元素。

输出 2:搜索不属于数组的元素。

搜索和全部搜索的区别:

下表列出了“搜索”和“全部搜索”之间的所有主要区别:

SEARCH

SEARCH ALL

SEARCH verb is used to perform a linear search in COBOL.SEARCH ALL verb is used to perform binary search in COBOL.
For this the array is not required to be in the sorted form. For this, the array must be in the sorted order form (either ascending or descending).
It performs searching operations sequentially and is also known as sequential search. It performs searching operations by comparing the element with the middle element of the array, divides the array into two halves, and continues the process until the element is found.
It performs slow operations and is less efficient.It performs a fast operation and is more efficient
It can be used in single as well as multi-dimensional arrays.It can only be used in single-dimensional arrays.
Multiple conditions can be applied for searching using the WHEN.Only one equality condition can be applied.
The value of identifier-1 must be initialized prior to the SEARCH verb using the SET verb.There is no need to use the SET verb prior to SEARCH ALL verb and thus the incrementing is done automatically.