Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Table Question

Status
Not open for further replies.

Slonoma

Programmer
Jul 10, 2003
41
US
Hey All,
When you do a search table, what is returned if the record you are looking for is not found?

Slo-No

The trouble with programmers is that they get high on their own supply. -Dimandja
 
here is the code

845500 SET MFWR-INDX TO 1.
845550 SEARCH MFWR-WHSE-COUNTRY-TBL
845600 WHEN (WHSE-WHS = MFWR-CNTRY-WHS (MFWR-INDX))
845650 AND (WHSE-BAY = MFWR-CNTRY-BAY (MFWR-INDX))
845700 AND (WHSE-ROW = MFWR-CNTRY-ROW (MFWR-INDX))
845750 NEXT SENTENCE

If no records are found matching this, what is MFWR-INDX's value?

The trouble with programmers is that they get high on their own supply. -Dimandja
 
If doing a SEARCH ALL it is not affected.
If doing a SEARCH I'm not sure. The manual I have is not very clear on this.

Any way I advise you to use the AT END option and to set this index to a value you are sure of, or to set a flag indicating that no occurrence was found.



Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Thanks for the quick reply Frederico,

I'm pretty new to COBOL and have never used a SEARCH table command. Does the AT END command go where NEXT SENTENCE is?

Thanks again,
Slo-No

The trouble with programmers is that they get high on their own supply. -Dimandja
 
No, the "NEXT SENTENCE" is telling you what to do when there is a MATCH. The "AT END" tells you what to do if there is NO match.

for example,

Code:
    SET MFWR-INDX       TO 1.                        
     SEARCH MFWR-WHSE-COUNTRY-TBL 
  At End
       Perform No-Match-Found                    
  When (WHSE-WHS = MFWR-CNTRY-WHS (MFWR-INDX))  
         AND (WHSE-BAY = MFWR-CNTRY-BAY (MFWR-INDX))  
         AND (WHSE-ROW = MFWR-CNTRY-ROW (MFWR-INDX))  
            NEXT SENTENCE

Bill Klein
 
I think it would be advisable for you to look at your COBOL reference manuals.

As you haven't mentioned which brand of COBOL I am going to give you two links to two makers.

Most of what you will find there is common to ALL actual COBOL versions.

RM/COBOL

IBM AS400 COBOL (COBOL/400)

IBM AS400 COBOL (ILE-COBOL)


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Normally with a linear SEARCH as you are doing, when you reach the end without finding a match, the index value is one more than the number of occurrences in the table. Just so you know, the index contains a displacement value to access the memory where your table is located but for all practical purposes, you can think of it as an occurrence. At any rate, you are pointing to the area of memory immediately following your table.
 
COBOLKenny,

Your post is dubious advice, at best.
At any rate, you are pointing to the area of memory immediately following your table.
This is plain wrong. One cannot assume that an invalid index value "points" to anything.


The most prudent practice in a format 1 (linear) SEARCH is to use the AT END phrase to set flags or do whatever your code expects, and do not rely on nonstandard side effects that might work on a particular implementation of COBOL.

Tom Morrison
 
Tom -

I don't have a copy of the standard handy, but my IBM COBOL Language Reference manual says:
If the end of the table is reached (that is, the incremented index-name value is greater than the highest possible occurrence number) without the WHEN condition being satisfied, the search is terminated, as described in the next paragraph.

If, when the search begins, the value of the index-name associated with identifier-1 is greater than the highest possible occurrence number, the search immediately ends, and, if specified, the AT END imperative-statement is executed. If the AT END phrase is omitted, control passes to the next statement after the SEARCH statement.
While one can't assume the index points to anything, it is clearly greater than the occurrence number and can be tested for such a value to determine if the SEARCH failed to satisfy its terminating condition:
Code:
IF MFWR-INDX > MFWR-WHSE-COUNTRY-TBL-SIZE
    ... search failed ...
Such a test should work properly regardless of compiler or platform.

Regards.

Glenn
 
Tom -

My prior post assumes that the index used for the SEARCH has a value greater than zero when the SEARCH verb executes. The 2002 standard speaks to this issue directly. I'm not sure that prior standards addressed it.

I should also say that I agree with you that, in general, the most prudent (clear and correct) approach is to use the AT END phrase.

Glenn
 
Tom:
I wasn't giving any advice. The question was what happens when a match is not found. As per Glenn's post, if none of the when conditions has been met, the index is incremented and if greater than the number of occurrences defined for the table the SEARCH ends.

I would never rely on something like that happening and code for it. I would always use the AT END condition to account for a non-match in the table.
 
Glenn and COBOLKenny,

I thought, by use of the quote mechanism, that it was clear I was referring to the issue of an index 'pointing to an area of memory'.

Tom Morrison
 
Thanks for the tips everyone!

I'm going to throw the AT END statement in there and see if that takes care of it. Sorry to cause such a commotion!

Slo-No

The trouble with programmers is that they get high on their own supply. -Dimandja
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top