View previous topic :: View next topic
|
Author |
Message |
swapnadeep.ganguly
Active User
Joined: 21 Mar 2007 Posts: 203 Location: India
|
|
|
|
Hi,
We have two files say A and B. They contains some records in them. Let us assume the details in the two files as
FILE A:
1
2
3
4
5
FILE B:
1
3
5
7
9
Now we are required to created 3 files X,Y and Z.
The File X should contain all the datas that are common to File A & B, while File Y should contain all the data that are present in A but not in B and File Z should contain all the data that are present in B but not in A i.e. the desired output for the three files are as under:
FILE X
1
3
5
FILE Y
2
4
FILE Z
7
9
How can we achieve the same using a COBOL program?
I thought of the logic to read the file A and then compare it with File B. If the records are found, then write the same to File X and if not found, write the same in File Y.
But how can we get File Z?
Its a bad idea to read file B and compare it with File A and then write the same into File Z. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
please do not double post
Your other post with the same subject will be deleted |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
just a hint....
the input files should be sorted on the field used for matching
if key1 = key2 ..... write file x read file1 and file2
if key1 < key2 ..... write file y read file1
if key1 > key2 ..... write file z read file2 |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
Store the records in an array while writing file X. Compare file B with this array. If the records are not found, then write to file Z. |
|
Back to top |
|
|
swapnadeep.ganguly
Active User
Joined: 21 Mar 2007 Posts: 203 Location: India
|
|
|
|
Thanks Enrico and Bharath for your prompt reply.... |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
Does that mean you are satisfied with the answer? Or do you have something else on your mind? |
|
Back to top |
|
|
swapnadeep.ganguly
Active User
Joined: 21 Mar 2007 Posts: 203 Location: India
|
|
|
|
No, I am satisfied with your answer. |
|
Back to top |
|
|
revel
Active User
Joined: 05 Apr 2005 Posts: 135 Location: Bangalore/Chennai-INDIA
|
|
|
|
Hi,
You can do in this way
First SORT FILE A and FILE B in ASCENDING ORDER (I Assume that there will unique field-about which you should sort).
Then
1. Read Record from FILE A.
2. Read Record from FILE B.
3. Put it in loop FILE A Read until End of file
4. Compare fields like
Code: |
> If FIELD A = FIELD B
write FILE A and FILE B records into FILE X
read FILE A
read FILE B
ELSE
IF FIELD A > FIELD B
write FILE A records into FILE Y
read FILE A
ELSE
write FILE B records into FILE Z
read FILE B
END-IF
END-IF |
5. Once End of File is reached, Close it.
Hope you clear |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
swapnadeep.ganguly wrote: |
How can we achieve the same using a COBOL program?
|
|
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
I suppose that you could always ask the interviewer why COBOL should be used when either main sort product could easily accomplish this task. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
revel - the logic posted is incomplete and will fail.
Quote: |
I suppose that you could always ask the interviewer why COBOL should be used when either main sort product could easily accomplish this task. |
In more than 99% of the cases where 2 files are matched, there is more to the code than simply "matching the files". There is nearly always other business logic (internal array processing, vsam access, database access, etc) which makes cobol a better choice than using the sort. I've seen implementations that someone who did not know how to code a simple 2-file match used 4 or more steps (and several full passes of the data) to accomplish what one fairly simple cobol program could do. The only reason i was asked to look at these processes was that they took "too long" to run. If you only have a few thousand records, i guess multiple steps/passes is ok, but much of what my clients work with are hundreds of millions of records. The testing (little bits of data) goes quite well and when full-volume testing is attempted it grinds to a halt.
Quote: |
Store the records in an array while writing file X. Compare file B with this array. If the records are not found, then write to file Z. |
This should not be done. A programmer should learn to write proper code.
swapnadeep.ganguly - at/near the top of the cobol part of the forum is a "Sticky" that contain working code for a 2-file match/merge. if you download and review that code, you should have a more complete understanding of the process. any questions, please post back here. |
|
Back to top |
|
|
revel
Active User
Joined: 05 Apr 2005 Posts: 135 Location: Bangalore/Chennai-INDIA
|
|
|
|
Hi Dick,
Quote: |
revel - the logic posted is incomplete and will fail. |
have to tested above logic, i don't think so, it will fail.....
Just i coded main logic part rest he as to think....
Ya.. it will fail in below code only when
Code: |
3. Put it in loop FILE A Read until End of file |
Ie he need to handle for EOF condition for both files else it will fo in loop.
Quote: |
First SORT FILE A and FILE B in ASCENDING ORDER (I Assume that there will unique field-about which you should sort). |
The above logoc is work only when Files were having records of unique type and were sorted about that fields in case If the Fisrt File is having duplicate records the logic will change
Any suggestion are welcomed[/quote] |
|
Back to top |
|
|
|