Filtering out Matching records from two work files

If we are reading two work files which are not in sorted order, we need to write the matching records into third file. What is the efficient way to do that?

I only have one approach -

Read work file 1
Read work file 2
If work file 1 policy = work file 2 policy
write work file 3
escape bottom
End-if
End-work
End-work

This approach seems to have performance issue. Please let me know if there is any efficient approach.

Sort both the two input files and code a two file match. The algorithm is simple.

Okay Giles! So you mean I will add a SORT step before executing Natural program and then use the same logic I posted here?

yes sort the two input files, if they have to be preserved sort to two temp inut files then.

Read WF1
Read WF2
Repeat until EOF WF1 and EOF WF2
if key WF1 = key WF2
write new file
else
if key WF1 < key WF2
read WF1
else
if key WF1 > WF2
read WF2
end-if
end-if
end-if
end-repeat

Check for EOF and set key to high values at EOF

Okay got it! So here Read WF1 and Read WF2 mean READ WORK FILE 1 ONCE and READ WORK FILE 2 ONCE … right?

Right!

READ WORK x ONCE
AT END
MOVE all H’FF’ TO WFxKEY
END-ENDFILE

The problem could be solved using sort only,

write the first of the duplicate pair to the output file which could then be processed by the next step,

avoids writing any code.

I think the OP has obtained all the answers to his interview questions/college project now. :roll:

Giles! You are right… :slight_smile:
I got many solutions but just waiting for trying everything. Unfortunately not having any platform to try them… I love discussing on this forum… will have many more topics to discuss once system is in my hand… :smiley:

It would probably have been polite to explain this when you started all your threads.
I’ll send you my invoice for consultancy now :wink: