Compare two files and write new file for add/delete/change



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

Compare two files and write new file for add/delete/change

Postby MSURESH309 » Mon Nov 10, 2014 6:57 am

I am writing a COBOL program to compare two files(sorted) Curr-file & Prev File.Both have layout as PersonID,DependentId,RelationType.

Single PersonId may have multiple DependentId with various relationship Types.

Whenever there is a change in RelationType I need to write a new o/p file as below.
PersonID,DependentId,RelationType, ADD/CHG/DEL, (--> need to add a indicator at end like ADD/CHG/DEL).
If there is no change in record then dont write.

Example :-

Prev-file have 10 records with one PersonId with various relationship Types for different DependentId
Curr File have 12 records with
6 unchanged records
2 new DependentId(which are not exists in prev file) and 2 new relation types
3 records have just change in relation types with old DependentId(as prev file)

Result file should contain

ADD - 2 records
CHG - 3 records
DEL - 1 record

I looked at example program ibm-cobol/file-match-merge-sample-code-t881.html, looks like it may not work as my file contains duplicate PersonId. Can anyone help me with some ideas?
Thanks.
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Compare two files and write new file for add/delete/change

 

Re: Compare two files and write new file for add/delete/chan

Postby BillyBoyo » Mon Nov 10, 2014 3:26 pm

You can adapt the code for duplicates.

However, what I do when matching with duplicates (and even with lesser excuse) is what I call a "sideways" two-file-match (also use for an ordinary match if I don't have an example to base it on).

Instead of reading both files at the same time, one file is the "driver". So in the logic for processing that file, you can have anything you need just like you were processing a single file. What you also have available to the current record on that file is the current record on the second file. You use the key-value comparison between the two files.

If file 1 < file 2, record only on file 1
if file 1 = file 2, match
otherwise, go "sideways" until file 2 not less than file 1 - which means process file 2 separately and independently, for record only on file 2.

Priming Read File 1
Priming Read File 2
Perform until EOF on File 1
  Evaluate True
      Key 1 = Key 2
        Perform match
      Key 1 < Key 2
       Perform On File 1 only
      Otherwise
        Perform On File 2 only until Key 1 <= Key 2
  End-Evaluate
End-Perform
Set Key 1 "high"
Perform On File 2 only until EOF File 2


Set the key "high" in On File 2 only if EOF is reached. Keep the keys in WORKING-STORAGE and you don't get in a tangle with EOF checking everywhere.

At heart, it is just a simplification of the two-file match. It pulls the code for File 2 processing "sideways" out of the match, so that both files can be treated as individual files which just happen to be being processed at the same time.

Pseudo-Code'd
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Compare two files and write new file for add/delete/chan

Postby MSURESH309 » Tue Nov 11, 2014 9:29 am

Thanks for the reply BillyBoyo. I'll work on the code and will keep you updated.
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post