Page 1 of 1

SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 4:00 pm
by Tecno
Hi, I have the following code:

SEARCH TI-PROD                           
  AT END                                       
     CONTINUE                                 
  WHEN TI-C-PROD(WS-INDEX-AUX) =     
       WS-C-PROD-AUX                 
   AND TI-C-OBRG-PROD(WS-INDEX-AUX) =     
       WS-C-OBRG-PROD-AUX                 
     PERFORM XXX-INSERT                           
           THRU XXX-INSERT-EXIT                     
END-SEARCH                                                 


Even when the WHEN conditions are true, I can never get it to enter the When clause instructions, and I can't understand why ?
Can anyone help please ? Thanks !

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 4:42 pm
by Robert Sample
Even when the WHEN conditions are true, I can never get it to enter the When clause instructions, and I can't understand why ?
Incorrect assumption 1: how do you KNOW (not guess) that the WHEN conditions are true?

Incorrect assumption 2: how do you KNOW (not guess) that the WHEN clause instructions are not being executed?

The COBOL SEARCH verb works as the manual indicates, so if your WHEN clause is not being triggered it is because you have no table entries that meet the WHEN clause conditions.

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 5:44 pm
by Tecno
I inserted displays, to see the values of the table elements and variables and they matched! Plus the table elements and variables are declared in the same way.

Also i changed the Search instruction for an Evaluate True statement and it worked!

Any ideas? Maybe if i change the order of the When clause and the At End clause? But its weird because in another part of this program, i have another search statement and the At End clause cames first also, and only after the When clauses, and it works...

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 5:57 pm
by Robert Sample
From the COBOL Language Reference manual:
Before executing a serial search, you must set the value of the first (or only) index associated with identifier-1 (the search index) to indicate the starting occurrence for the search.
If WS-INDEX-AIX is not the index associated with the table, or you did not use a SET WS-INDEX-AIX TO 1 in your code before the SEARCH statement, your SEARCH could easily fail.

Changing the order of the WHEN clause won't make any difference. Such a change is what I call "religious programming" -- you have faith that changing something will cause you to get the results you want without any underlying basis for that belief. Before you change anything you need to understand why you are not getting the expected results, which will tell you what to change based on logic.

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 6:44 pm
by Tecno
No, WS-INDEX-AUX is a variable, not the index of the table...

Changing the order of the WHEN clause won't make any difference. Such a change is what I call "religious programming" -- you have faith that changing something will cause you to get the results you want without any underlying basis for that belief. Before you change anything you need to understand why you are not getting the expected results, which will tell you what to change based on logic.


Funny you should say this, because if you code an Evaluate Statement and the When Other, doesn't come after the other Whens, compilation fails ! ;) How's this for "religious programming" eh ? :)

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 7:04 pm
by Robert Sample
Find a copy of the COBOL Language Reference manual and read section 6.2.32 on the SEARCH statement. If you read it long enough, you will figure out what you are doing wrong. You obviously are not interested in getting help for your problem, since you're comparing EVALUATE and SEARCH as if they are interchangeable (they are not), so I'll let you figure out your problem on your own.

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 7:52 pm
by Tecno
Robert Sample wrote:Find a copy of the COBOL Language Reference manual and read section 6.2.32 on the SEARCH statement. If you read it long enough, you will figure out what you are doing wrong. You obviously are not interested in getting help for your problem, since you're comparing EVALUATE and SEARCH as if they are interchangeable (they are not), so I'll let you figure out your problem on your own.


I want help of course ! ;) And I thank you for your help ! I will try to find that document and read that section.
In this particular case, I changed the Search for the Evaluate True and it worked for me, I didn't say it works for every case.

Thanks for the help!

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 8:05 pm
by Robert Sample
SEARCH allows you to evaluate every element of the array against your criteria.

EVALUATE, unless you have a loop iterating across the array that you have not displayed, evaluates one single array element against the criteria.

This is a significant difference -- especially if the matched item is the last element in the array and your subscript variable is pointing to the first element of the array.

Re: SEARCH: never enters the WHEN clause, even if true...

PostPosted: Thu Oct 14, 2010 8:27 pm
by Tecno
I've figured out the problem... Shouldn't really be using the SEARCH statement ! Also if I wanted to use the Search statement, I should have used the Index of the internal table and not one specified by me just in a variable.

Thanks for all the help ! :)