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



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

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

Postby Tecno » Thu Oct 14, 2010 4:00 pm

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 !
Tecno
 
Posts: 9
Joined: Thu Oct 14, 2010 3:50 pm
Has thanked: 0 time
Been thanked: 0 time

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

 

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

Postby Robert Sample » Thu Oct 14, 2010 4:42 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

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

Postby Tecno » Thu Oct 14, 2010 5:44 pm

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...
Tecno
 
Posts: 9
Joined: Thu Oct 14, 2010 3:50 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby Robert Sample » Thu Oct 14, 2010 5:57 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

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

Postby Tecno » Thu Oct 14, 2010 6:44 pm

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 ? :)
Tecno
 
Posts: 9
Joined: Thu Oct 14, 2010 3:50 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby Robert Sample » Thu Oct 14, 2010 7:04 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

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

Postby Tecno » Thu Oct 14, 2010 7:52 pm

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!
Tecno
 
Posts: 9
Joined: Thu Oct 14, 2010 3:50 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby Robert Sample » Thu Oct 14, 2010 8:05 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

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

Postby Tecno » Thu Oct 14, 2010 8:27 pm

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 ! :)
Tecno
 
Posts: 9
Joined: Thu Oct 14, 2010 3:50 pm
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post