IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Creating 3 files


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
swapnadeep.ganguly

Active User


Joined: 21 Mar 2007
Posts: 203
Location: India

PostPosted: Tue Sep 16, 2008 3:40 pm
Reply with quote

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
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Sep 16, 2008 3:43 pm
Reply with quote

please do not double post
Your other post with the same subject will be deleted
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Sep 16, 2008 3:47 pm
Reply with quote

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
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Sep 16, 2008 3:50 pm
Reply with 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.
Back to top
View user's profile Send private message
swapnadeep.ganguly

Active User


Joined: 21 Mar 2007
Posts: 203
Location: India

PostPosted: Tue Sep 16, 2008 4:01 pm
Reply with quote

Thanks Enrico and Bharath for your prompt reply....
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Sep 16, 2008 4:03 pm
Reply with quote

Does that mean you are satisfied with the answer? Or do you have something else on your mind?
Back to top
View user's profile Send private message
swapnadeep.ganguly

Active User


Joined: 21 Mar 2007
Posts: 203
Location: India

PostPosted: Tue Sep 16, 2008 4:06 pm
Reply with quote

No, I am satisfied with your answer.
Back to top
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Tue Sep 16, 2008 4:06 pm
Reply with quote

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
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Sep 16, 2008 4:24 pm
Reply with quote

swapnadeep.ganguly wrote:
How can we achieve the same using a COBOL program?
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Sep 16, 2008 4:57 pm
Reply with 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.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Sep 16, 2008 9:45 pm
Reply with quote

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
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Wed Sep 17, 2008 11:19 am
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> Mainframe Interview Questions

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 2
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts Write line by line from two files DFSORT/ICETOOL 7
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Merge two VSAM KSDS files into third ... JCL & VSAM 6
Search our Forums:

Back to Top