Valid and invalid Key operation in Cobol



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

Valid and invalid Key operation in Cobol

Postby nm992 » Sat Oct 22, 2011 12:43 pm

I have a input file with the following format

Name Age Action type
a 23 Add
b 24 delete
c 24 delete

Where name is the key..I need to read this file (f1) and do action on another file(f2) depending of reading action action type

I am coding as below.

READ F2 KEY IS in-name
IF IN-ACTION-TYPE='ADD'
INVALID KEY PERFORM Para1
NOT INVALID KEY PERFORM para 2
ELSE IF IN-ACTION-TYPE='DELETE'
INVALID KEY PERFORM para 3
NOT INVALID KEY PERFORM para 4
END-IF
END-READ.

While compiling the code I am getting the error

EXPECTED A VERB OR "NEXT SENTENCE", BUT FOUND "INVALID"

Can anyone please suggest me how to correct this..
nm992
 
Posts: 43
Joined: Sun Jul 11, 2010 11:31 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Valid and invalid Key operation in Cobol

 

Re: Valid and invalid Key operation in Cobol

Postby BillyBoyo » Sat Oct 22, 2011 3:28 pm

You are mixing parts of the READ and the IF. You need something like this:

READ F2  KEY IS in-name                     
  INVALID KEY
    Do something
  NOT INVALID KEY
    Do something
END-READ.   

IF IN-ACTION-TYPE EQUAL TO 'ADD'                               
    Do something
ELSE
    IF IN-ACTION-TYPE EQUAL TO 'DELETE'                       
        Do something
    END-IF
END-IF                                               


Check your Cobol manuals/course notes to ensure that you understand these fully. I have added a second END-IF, because you had two IFs. I always check the "file status" rather than use VALID/INVALID KEY, but you do need to know how to do both. I also expanded the "=" and spaced things a little. The easier your code is to read, the easier it is to understand, the easier it is to get right. It might seem to take longer to write EQUAL TO than =, but once you get experience with an editor, you'll not notice the difference.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Valid and invalid Key operation in Cobol

Postby nm992 » Sat Oct 22, 2011 3:43 pm

Thanks..

But I need to read the key..and then perfrom different perform depending on action types..Even Valid and invalid values will be diff depending on action type..how can i achieve that..
nm992
 
Posts: 43
Joined: Sun Jul 11, 2010 11:31 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Valid and invalid Key operation in Cobol

Postby BillyBoyo » Sat Oct 22, 2011 3:57 pm

If you want to do it with the INVALID KEY, then you have to set a "flag" or "marker".

INVALID KEY
    MOVE "I" TO W-VALID-INVALID-FLAG
NOT INVALID KEY
    MOVE "V" TO W-VALID-INVALID-FLAG


Then you can have an 88 on the flag, and use the 88 in a "compound" IF or a "nested" IF.

Using file status instead of INVALID KEY and it would be fine with an 88 on the file status field.

Note, you can use the SET verb in place of the move, by changing the name of the flag to the name of the 88 that you use for the value:

SET W-KEYED-READ-INVALID TO TRUE
...
SET W-KEYED-READ-VALID TO TRUE
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Valid and invalid Key operation in Cobol

Postby dick scherrer » Sun Oct 23, 2011 8:32 am

Hello,

Even Valid and invalid values will be diff depending on action type..
Maybe i am reading this incorrectly, but this is wrong. . . The value will be returned on every read and will be consistent. It has nothing to do with what you want to do later depending on the add/delete/whatever.

If the key is not found - set an indicator to be used later (as already suggested). If the read otherwise fails, present a message and end the program. The OPEN should have a status check also (every vsam command should).
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Valid and invalid Key operation in Cobol

Postby GuyC » Mon Oct 24, 2011 5:30 pm

If you really want it in-line :
READ F2 KEY IS in-name
INVALID KEY
   EVALUATE I-ACTION-TYPE
       WHEN 'ADD'  PERFORM Para1
       WHEN 'DELETE' PERFORM para 3 
       WHEN OTHER continue
   END-EVALUATE
NOT INVALID KEY
   EVALUATE I-ACTION-TYPE
       WHEN 'ADD'  PERFORM Para2
       WHEN 'DELETE' PERFORM para 4
       WHEN OTHER continue
   END-EVALUATE
END-READ
GuyC
 
Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

Re: Valid and invalid Key operation in Cobol

Postby BillyBoyo » Mon Oct 24, 2011 5:43 pm

Just realised myself,

Even Valid and invalid values will be diff depending on action type


Since the action type is on the file, and the invalid key means you haven't been able to locate a record on the file, then it is not possible for you to take action based on the action type if you have invalid key...
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Valid and invalid Key operation in Cobol

Postby GuyC » Mon Oct 24, 2011 6:06 pm

action type is on File1, invalid key on File2
I can explain it to you, but i can not understand it for you.
GuyC
 
Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

Re: Valid and invalid Key operation in Cobol

Postby BillyBoyo » Mon Oct 24, 2011 6:50 pm

Sorry, got it now. Thanks Guy.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post