Saturday, March 21, 2015

SEARCH and SEARCH ALL in COBOL with examples

SEARCH is a serial search.It can be used for both sorted and unsorted data inside the table.The table needs to be defined with an index .When we use the search function, the function starts with the current setting of the index and it goes on until to the end of the table.The starting value of the index is left upto the choice of the programmer. We can set the value of the index value just before the search starts using the SET keyword.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
WORKING-STORAGE SECTION.
   01 WS-TABLE.
          05 WS-TEST PIC X(1) OCCURS 10 TIMES INDEXED BY I.

   01 WS-SRCH-STRNG PIC A(1) VALUE 'R'.

PROCEDURE DIVISION.
   MOVE 'ABCDEFRJKL' TO WS-TABLE.
   SET I TO 1.
   SEARCH WS-TEST
     AT END DISPLAY   'R NOT FOUND IN TABLE'
     WHEN WS-TEST(I)=WS-SRCH
     DISPLAY    'WE FOUND YOU IN THE TABLE'
   END-SEARCH.  

STOP RUN.

This linear Search will iterate through the entire table until the search-string is found or the end of table is reached.
In this case, since our search string 'R' is there in the table, the SYSOUT will display 'WE Found.. '.

SEARCH ALL is a binary search and the table must be sorted .That is the the prerequisite to use search all. The outcome of SEARCH ALL is either a yes or a no. Hence we can not code multiple search conditions when using SEARCH ALL function.
Contrary to search function, there is no need to initialize the index .

Check the following code:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-TABLE.
      05 WS-BIN-SRCH OCCURS 10 TIMES ASCENDING KEY IS WS-NUM INDEXED BY I.
      10 WS-NUM  PIC 9(2).
      10 WS-DEP   PIC A(3).

PROCEDURE DIVISION.
   MOVE '12ABC56DEF34GHI78JKL93MNO11PQR' TO WS-TABLE.
   SEARCH ALL WS-BIN-SRCH
     AT END DISPLAY 'BIN-SRCH NOT FOUND'
     WHEN WS-NUM (I)=93
     DISPLAY 'BIN-SRCH FOUND '
     DISPLAY WS-NUM(I)
     DISPLAY WS-DEP(I)

Just a point to note:  The table is searched in ASCENDING ORDER. If you see, the table is defined with ASCENDING key clause. In case we want, we can always define the table in DESCENDING KEY clause.
To use SEARCH ALL,a field must be in ASCENDING or DESCENDING key clause. This Identifier must be an entry within the table

When to use SEARCH and SEARCH ALL Function:
· For a table with less than 50 entries, go for SEARCH (Sequential Search)
· For a table with more than 50 entries go for SEARCH ALL (Binary Search)

10 comments:

  1. Just a doubt in the example you have given for binary search, You have mentioned in WS section that WS-NUM is indexed by I, whereas in procedure division its WHEN WS-ROLL(I)=93. Could you please clarify with another example.

    ReplyDelete
    Replies
    1. Hi..Thanks for you comment and finding out the typo. It was incorrect there. It should be:
      01 WS-TABLE.
      05 WS-BIN-SRCH OCCURS 10 TIMES ASCENDING KEY IS WS-NUM INDEXED BY I.
      10 WS-NUM PIC 9(2).
      10 WS-DEP PIC A(3).

      To use SEARCH ALL,a field must be in ASCENDING or DESCENDING key clause. This Identifier must be an entry within the table. I have updated the code..
      Please let me know if this clarifies. Thanks again!

      Delete
    2. Thank you for clarifying it.

      Delete
  2. Will the program fail if the search all is used on unsorted table

    ReplyDelete
    Replies
    1. No, program won't fail. Even if the item exists in the table, search all would not find it.

      Delete
  3. For the Example in the Search all, the Key is in ASC order but while moving the data in the string you did not move the data in ASC order. Would it cause any error or the system does the sort internally ?

    ReplyDelete
    Replies
    1. Hi..I faced this situation once. Search all wont sort the data on its own. What happens is, even if the data is present in the table, program would not be able to find it.

      Delete
  4. Hi in my cobol program we are using 3 arrays size(883011)each n fetching the data using search all. My program is perfectly fetching the data from first two arrays using search all but for the third array search all is not working only search is working.what can b the reason?

    ReplyDelete
    Replies
    1. Check if the data is sorted. Search all works only when data is sorted

      Delete
  5. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete