Joined: 29 Sep 2008 Posts: 6 Location: United Kingdom
I have a query related to sequential search in COBOL.
What would be the position on index when a sequential search is not successful.
Is it the last position ?
n+1 where n is the last position where data in stored in a table
Ex - A table with OCCURS 10 INDEXED BY I.
Suppose there is data in 5 positions.
If search is not successful the position of index is 10,6,5. I read somewhere that it is unpredictable.
Joined: 06 Jun 2008 Posts: 8201 Location: Dubuque, Iowa, USA
The COBOL Language Reference (manuals link at the top of the page), section 220.127.116.11 on sequential SEARCH, is quite clear:
If the end of the table is reached (that is, the value of the incremented index is greater than the highest possible occurrence number) without the WHEN condition being satisfied, the search is terminated.
In other words, the value will be 11 for your example.
Please note that the actual number of elements you've loaded into the table has no bearing upon the SEARCH verb. Only the defined number of elements (10 in your example) matters -- the fact that you've loaded only 5 elements means that you're comparing unknown values for the last 5 search tests (possibly LOW-VALUES, possibly something else).
If a BINARY search fails, the index value will be unpredictable. However, if a SEQUENTIAL search fails the index value is known.