Reverse the Data records in a File



Reverse the Data records in a File

Postby kk12345 » Sun Jan 23, 2011 9:55 pm

Dear Experts,

I was asked an interview Question recently, which is as follows:

I have a file with a Header and 25 data records (assume Fixed length records). What is the logic to reverse the order of the data-records.
Let's take an example for better understanding.
Input file should like :
Header 2011/01/23 15
record 1
record 2
record 3
record 4
record 5
record 6
record 7
record 8
record 9
record 10
record 11
record 12
record 13
record 14
record 15

Output file should look like

record 15
record 14
record 13
record 12
record 11
record 10
record 9
record 8
record 7
record 6
record 5
record 4
record 3
record 2
record 1
Header 2011/01/23 15


How you can achieve this when the i/p file
1) Sequential File
(i) Using COBOL program (ii) Using JCL
2) VSAM File
(i) Using COBOL program (ii) Using JCL

Please answer this Question
kk12345
 
Posts: 18
Joined: Sun Dec 26, 2010 6:26 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Reverse the Data records in a File

Postby Robert Sample » Sun Jan 23, 2011 10:11 pm

Sequential file:
You can use sort to add a sequence number to the record and sort on descending sequence number. The same approach would work for COBOL, using INPUT PROCEDURE to add the sequence numbers.

VSAM file:
If you are talking about ESDS, you'd have to unload the file to a sequential file, then apply that approach, then delete and redefine the ESDS file, loading it from the sorted file.
If you are talking about KSDS, it cannot be done. The key of a KSDS file is kept in ascending sequence by VSAM and you cannot change that.
If you are talking about RRDS, I doubt it can be done at all; if it can, it would be something along the lines of the ESDS.
If you are talking about linear data sets in VSAM, the question makes no sense.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Reverse the Data records in a File

Postby kk12345 » Sun Jan 23, 2011 10:14 pm

thank you Robert Sample.
kk12345
 
Posts: 18
Joined: Sun Dec 26, 2010 6:26 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Reverse the Data records in a File

Postby NicC » Mon Jan 24, 2011 10:57 am

And you CANNOT do it in JCL at all as JCL does not manipulate data. The programs it notifies to the processor do the manipulation.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times


Return to Interview Questions

 


  • Related topics
    Replies
    Views
    Last post