Cobol

SEARCH and SEARCH ALL use in Cobol Program

LINEAR and BINARY searches can be coded using traditional code or using the SEARCH or SEARCH ALL verb. The one that is coded using traditional code uses an Occurs to define the re-occurrence of the elements in the table and the Subscript to control stepping through the table one element at a time using the looping.

A subscript is essentially a pointer that points to one of the elements in the table. The subscript is a traditional field under the control of the programmer. The subscript is usually initialized with a MOVE statement and incremented with an ADD statement (or a SUBTRACT statement if the subscript was being decreased).

In contrast to the subscript, the SEARCH verb uses an index which is defined by name with the table. The actual setup of the index is done through COBOL. The index is limited in use. It must be used with the table that defined it and it must be controlled using either the PERFORM statement with the varying clause or the SET verb. Essentially a programmer has far more control over the manipulation of a subscript than an index. Because the index is used with the SEARCH verb, the restrictions are defined to maximize its effective use in that context.

SEARCH {table-name} [VARYING {index-name}]             
                [AT END processing-code-1]     
       {WHEN condition {processing-code-2}                     
                       {NEXT SENTENCE    }
[END-SEARCH] 

Example:
         SEARCH ENTRIES        
                AT END            
                   MOVE ERROR-MSG-TBL TO ITEM-NAME-PR        
                WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN           
                   MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR

A linear search checks the field we need to match against each element in the table to see if they match. Binary Search is on a sorted array. Compare the item to be searched with the item at the center. If it matches, it stops the search else repeat the process with the left half or the right half depending on where the item lies. Based on this decision, we have eliminated half the table from the search. We next look at the middle item in the half that is left and compare it to the element. This will either find a match or eliminate another quarter of the table. Note that in establishing what the middle is if the number of elements is even, the programmer of a binary search can choose to round or truncate. This process continues until either a match is found or it is clear that the element is not in the table.

SEARCH Concept

  • It finds in a table sequentially starting at the element pointed to by the table index.
  • The starting value of the table index is under the control of the programmer. The programmer must ensure that, when the SEARCH executes, the table index points to some element in the table (for instance, it cannot have a value of 0 or be greater than the size of the table).
  • The VARYING phrase is only required when we require data-item to mirror the values of the table index. When the VARYING phrase is used, and the associated data-item is not the table index, then the data-item is varied along with the index.
  • The AT END phrase allows the programmer to specify an action to be taken if the searched for the item is not found in the table.
  • When the AT END is specified, and the index is incremented beyond the highest legal occurrence for the table (i.e. the item has not been found), then the statements following the AT END will be executed and the SEARCH will terminate.
  • The conditions attached to the SEARCH are evaluated in turn and as soon as one is true the statements following the WHEN phrase are executed and the SEARCH ends.

Rule of the Thumb

  • For a table with less than 50 entries, go for SEARCH (Sequential)
  • For a table with more than 50 entries go for SEARCH ALL (Binary)
SEARCH ALL {table-name} [VARYING {index-name}]             
         [AT END processing-code-1]     
{WHEN condition {processing-code-2}                     
                {NEXT SENTENCE    }
[END-SEARCH] 

Example:
     SEARCH ALL ENTRIES        
     AT END            
     MOVE ERROR-MSG-TBL TO ITEM-NAME-PR       
WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN           
     MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR

SEARCH ALL Concept

When the SEARCH ALL is used, the programmer does not need to set the table index to a starting value because the SEARCH ALL controls it automatically.

Advantages and Disadvantages 

  • SEARCH ALL access is faster than SEARCH.
  • If the table to be searched is only partially populated, the SEARCH ALL may not work correctly. If the table is ordered in ascending sequence then, to get the SEARCH ALL to function correctly, the unpopulated elements must be filled with HIGH-VALUES. If the table is in a descending sequence, the unpopulated elements should be filled with LOW-VALUES.

SET verb used to manipulate table indexes. An index item has a special internal representation designed to optimize searching, it cannot be used in any type of normal arithmetic operation (ADD, SUBTRACT, etc.) or even in a MOVE or DISPLAY statement. For index items, all these operations must be handled by the SET verb.

The versions of the SET verb shown below are used to assign a value to an index item, to assign the value of an index item to a variable, and to increment or decrement the value of an index item.

SET {index-name-1} TO {index-name-2}

    {data-name-1 } TO {data-name-2 }

                      {integer     }

There is also a special format for adding to or decreasing the index:

SET {index-name} {UP BY  }   {data-name}

                 {DOWN BY}   {integer  }
SEARCHSEARCH ALL
It is also called a linear or sequential .It is also called a binary .
The entries do not need to be in any order.The table entries must be in some order.
Initialization & incrementing of an index is required.Only initialization is required of an index. Incrementing is done automatically by the system.
SET statement is a must for initializing the index before its usage.SET statement is not required.
Multiple when conditions can be coded.Only one WHEN condition can be coded.
Multiple arithmetic operators like =, >, <, ><=, >=, NOT= can be used.Only = operator is allowed.
Access is slow.Access is faster.
It can be used for both single-dimensional & multidimensional arrays.It is used for only single-dimensional arrays.
Data inside an array need not be in sorted order.Data must be in sorted order.

COBOL Blog: Click Here IBM Reference:Click Here

Admin

Share
Published by
Admin

Recent Posts

Increase Transparency and Collaboration Product Backlog

A well-maintained product backlog is crucial for successful product development. It serves as a single…

3 weeks ago

Product Backlog – Incremental value to the customer

Incremental value to the customer refers to the gradual delivery of small, functional parts of…

4 weeks ago

Product Market, Customer’s Desire, Need, and Challenges

A Product Market refers to the group of potential customers who might be interested in…

4 weeks ago

PAL-EBM Professional Agile Leadership – EBM Certification

The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…

4 months ago

PAL I Professional Agile Leadership Certification

The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…

4 months ago

Scrum Master Certification: CSM, PSM, SSM

Choosing the right Scrum Master Certification depends on your current experience and career goals. If…

6 months ago