View previous topic :: View next topic
|
Author |
Message |
mandator10
New User
Joined: 20 May 2007 Posts: 7 Location: Mumbai
|
|
|
|
I have 3 files . 2 files have millions of records.These 2 files have one ( empnumber) common record field. i want to put those records in 3rd file , such that which records having same number in key field (empnumber)in these two files ?
How will you write a logic for this in cobol?
One way is:
Read file1 until EOF1 or EOF2
Read File2 Until EOF2 or File1-Key >File2-key
If File1-key = File2-key
Write .....
End-If
End-Read
End-Read.
But READ stmt dont have UNTIL.
So it is to be done thru PERFORM stmt.
Can anyone write a full logic in cobol? (Dont suggest ICETOOL)
Savvy |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
Why use cobol when sort can do this for you ? |
|
Back to top |
|
|
Devzee
Active Member
Joined: 20 Jan 2007 Posts: 684 Location: Hollywood
|
|
Back to top |
|
|
mandator10
New User
Joined: 20 May 2007 Posts: 7 Location: Mumbai
|
|
|
|
Hi expat,
Several approaches are there. But their requirement is to write the logic in cobol. I strucked up. That is why i asked the question here.
Savvy |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
A couple of days ago i posted code that basically does what you need.
Look in COBOL for a recent topic about comparing/matching (i forget which) files that has an attachment. |
|
Back to top |
|
|
krisprems
Active Member
Joined: 27 Nov 2006 Posts: 649 Location: India
|
|
|
|
mandator10
Check this code, where FILE1 is 1st i/p file, and IE is 2nd I/p file.
OP is O/p file
Code: |
PROCEDURE DIVISION.
PERFORM 100-OPEN-PARA.
PERFORM 200-READ-PARA.
PERFORM 250-COMP-PARA UNTIL EOF-IE = 'Y' OR EOF-FILE1 = 'Y'.
PERFORM 900-CLOSE-PARA.
100-OPEN-PARA.
OPEN OUTPUT OP.
OPEN INPUT FILE1 IE.
200-READ-PARA.
READ IE AT END MOVE 'Y' TO EOF-IE.
READ FILE1 AT END MOVE 'Y' TO EOF-FILE1.
250-COMP-PARA.
IF(EMP-NUM-2 = EMP-NUM-1) THEN
MOVE SPACES TO OP-REC
WRITE OP-REC FROM IE-REC
PERFORM 800-CHECK-PARA
PERFORM 200-READ-PARA
END-IF.
IF(EMP-NUM-2 > EMP-NUM-1) THEN
MOVE SPACES TO FILE1-REC
PERFORM 800-CHECK-PARA
READ FILE1 AT END MOVE 'Y' TO EOF-FILE1
END-IF.
IF(EMP-NUM-2 < EMP-NUM-1) THEN
MOVE SPACES TO IE-REC
PERFORM 800-CHECK-PARA
READ IE AT END MOVE 'Y' TO EOF-IE
END-IF.
800-CHECK-PARA.
IF(EOF-FILE1 = 'Y' OR EOF-IE = 'Y')
PERFORM 900-CLOSE-PARA
END-IF.
900-CLOSE-PARA.
CLOSE OP IE FILE1.
STOP RUN.
|
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello Krisprems,
Did you test this before posting?
At a quick glance, i believe this code will not work as needed in all cases. |
|
Back to top |
|
|
krisprems
Active Member
Joined: 27 Nov 2006 Posts: 649 Location: India
|
|
|
|
Hi dick,
The code that i have pasted is tested one, and have used it many a times.
Am too feeling that, it may not work in all cases as you said!
So, the constraint to use this code would be:
1. I/p's should be sorted.
2. I/P from one file should not have more than one matching records with second file. |
|
Back to top |
|
|
cvadlamudi
New User
Joined: 19 Apr 2007 Posts: 68 Location: India
|
|
|
|
Hi Kris
can u please chk this
Define a flag :
ws-acct-match pic x.
match-fnd value 'y'
match-nfnd value 'n'
1)SORT SORT-FILE1 ASCENDING SORT-KEY(empnumber) USING INPUT1-FILE GIVING INP1-FILE.
2)SORT SORT-FILE2 ASCENDING SORT-KEY(empnumber) USING INPUT1-FILE GIVING INP2-FILE.
PERFORM 100-OPEN-PARA. (do same as u mention)
PERFORM 200-READ-PARA. (do same as u mention)
PERFORM 250-COMP-PARA UNTIL EOF-IE = 'Y' OR EOF-FILE1 = 'Y'.
PERFORM 900-CLOSE-PARA.
250-COMP-PARA.
EVALUATE TRUE
WHEN INPFILED1 = INPFIELD2
move 'y' to ws-acct-match
WRITE OUTREC FROM INPUT1 OR INPUT2
READ 2ND FILE at end set flag to 'Y' end-read
perform until MATCH-NFND
if INPFILED1 not = INPFIELD2
move 'N' TO ws-acct-match
end-if
end-perform
WHEN INPFILED1 < INPFIELD2
READ 1ST FILE at end set flag to 'y' end-read
WHEN INPFILED1 > INPFIELD2
READ 2ND FILE at end set flag to 'y' end-read
END-EVALUATE.
Regards,
Mouli |
|
Back to top |
|
|
tosaurabh20
New User
Joined: 08 Jun 2007 Posts: 26 Location: Noida
|
|
|
|
krisprems wrote: |
Hi dick,
The code that i have pasted is tested one, and have used it many a times.
Am too feeling that, it may not work in all cases as you said!
So, the constraint to use this code would be:
1. I/p's should be sorted.
2. I/P from one file should not have more than one matching records with second file. |
Hi Krisprems,
The above two constraints can be avoided if we incorporate nested loops in the above logic.
Though it will be bit time consuming but will cover up all constraints and provide you a perfect output as desired.
Let me know in case of any issues.
Thanks and Regards
Saurabh |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Anything can be made more difficult than it is. . . .
Please find and review the code i posted previously (a different topic) and suggested earlier in this topic. Later this week, i'll try to make it a "sticky" or somehow get it to the "downloads" area.
There is no need to nave nested loops and convoluted logic to accomplish reading the files.
Do not do 2 sort using/givings an a program - actually, 1 using/giving is poor coding practice. . . If there is work to be done in an input or output procedure, the internal sort is often an excellent choice. To merely put some data in a sequence, the external sort is a better choice.
As was mentioned by someone earlier, if there may be duplicates in both files, that condition needs a bit of extra consideration - not because of code (if done correctly), but because of business rules. When this happens, there needs to be a clear definition of how things should be processed. Fortunately, mnost of the time, there will be duplicates in only one (or neither) input file. |
|
Back to top |
|
|
|