Page 1 of 1

Valid and invalid Key operation in Cobol

PostPosted: Sat Oct 22, 2011 12:43 pm
by nm992
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..

Re: Valid and invalid Key operation in Cobol

PostPosted: Sat Oct 22, 2011 3:28 pm
by BillyBoyo
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.

Re: Valid and invalid Key operation in Cobol

PostPosted: Sat Oct 22, 2011 3:43 pm
by nm992
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..

Re: Valid and invalid Key operation in Cobol

PostPosted: Sat Oct 22, 2011 3:57 pm
by BillyBoyo
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

Re: Valid and invalid Key operation in Cobol

PostPosted: Sun Oct 23, 2011 8:32 am
by dick scherrer
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).

Re: Valid and invalid Key operation in Cobol

PostPosted: Mon Oct 24, 2011 5:30 pm
by GuyC
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

Re: Valid and invalid Key operation in Cobol

PostPosted: Mon Oct 24, 2011 5:43 pm
by BillyBoyo
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...

Re: Valid and invalid Key operation in Cobol

PostPosted: Mon Oct 24, 2011 6:06 pm
by GuyC
action type is on File1, invalid key on File2

Re: Valid and invalid Key operation in Cobol

PostPosted: Mon Oct 24, 2011 6:50 pm
by BillyBoyo
Sorry, got it now. Thanks Guy.