Page 3 of 3

Re: How to Merge records in 2 ps files alternately into 3rd?

PostPosted: Wed Jan 11, 2012 9:38 pm
by BillyBoyo
What I am assuming is that:

Initially the first record is read from each of the SORTINxx files.

INREC, when present, intervenes and effectively the input records from the files become new input records to the merge process.

The merge process decides which key is lowest and passes that for output, via OUTFIL, OUTREC processing, whatever, if present.

The merge process then requests the next record from the file which had the lowest key.

The file to which that corresponds is read, INREC gets the record and makes a new record to pass to the merge process.

The flip-flopping is only happening because as each new input is processed, INREC assigns the next highest sequence number. The INREC will have been processed with each initial record from the files, prior to the merge processing being entered.

Internally to DFSORT, I'm not saying that I know how the "requests the next record" or anything works (is it pushed or pulled, as it were) but my solution was on the above assumptions. If it doesn't work like that, my solution is a bad one.

I had looked at the EQUALS processing for the MERGE to see if that would "get in the way", but since the keys are unique/unique-when the-merge-is-operating (like the suggestion that it would still work with a single digit) I used NOEQUALS, but with EQUALS merge would respect the order of the record appearing on the files when keys are equal - so, if SORTIN01 and SORTIN03 have equal merge keys, EQUALS will ensure the SORTIN01 record appears on the output before the SORTIN03 (assuming multiple-more-than-two SORTINxx's) record, whereas NOEQUALS would take whichever was the most convenient at that time.

Re: How to Merge records in 2 ps files alternately into 3rd?

PostPosted: Wed Jan 11, 2012 10:25 pm
by enrico-sorichetti
seems reasonable !

Re: How to Merge records in 2 ps files alternately into 3rd?

PostPosted: Wed Jan 11, 2012 11:34 pm
by Frank Yaeger
BillyBoyo has it essentially right.

For a MERGE, DFSORT starts out by reading the first record from SORTIN01 and the first record from SORTIN02 in that order.

INREC would thus assign seqnum 1 to SORTIN01 record 1 and seqnum 2 to SORTIN02 record 1.

DFSORT selects the lowest MERGE key of the pair (the "winner") which would be seqnum 1 and writes that record to the output data set. DFSORT then goes back to the same file which the winner came from (SORTIN01) and reads the next record (SORTIN01 record 2). INREC would thus assign seqnum 3 to SORTIN01 record 2. Now we have records with seqnum 2 and seqnum 3, so seqnum 2 is the winner and DFSORT writes that record to the output data set. Then it goes to SORTIN02 (the winner) to get the next record, and so on. So it works because DFSORT handles pairs of records from the two files based on the winner for each pair and the winner for each pair is always SORTIN01 and then SORTIN02 since they have alternating sequence numbers.

EQUALS has no effect in this case because all of the seqnums are unique.

So the process is:

READ RECORD1 from SORTIN01 - assign seqnum 1.
READ RECORD4 from SORTIN02 - assign seqnum 2.
Select winner for pair (seqnum 1/seqnum 2 -> seqnum 1) and write RECORD1 (winner) to SORTOUT.
READ RECORD2 from SORTIN01 (previous winner) - assign seqnum 3.
Select winner for pair (seqnum 2/seqnum3 -> seqnum2) and write RECORD4 (winner) to SORTOUT.
READ RECORD5 from SORTIN02 (previous winner) - assign seqnum 4.
Select winner for pair (seqnum 3/seqnum4 -> seqnum3) and write RECORD2 (winner) to SORTOUT.
and so on.

Does that help?

Re: How to Merge records in 2 ps files alternately into 3rd?

PostPosted: Thu Jan 12, 2012 12:28 am
by enrico-sorichetti
Thank You Frank!
as usual Your explanations are very clear.

Re: How to Merge records in 2 ps files alternately into 3rd?

PostPosted: Thu Jan 12, 2012 10:27 am
by Nik22Dec
Hi Billy, Dick, Enrico & Frank,

Thank you all for your quick replies. Now I understand!! Really Appreciate!!! :)