Page 1 of 1

Reverse the Data records in a File

PostPosted: Sun Jan 23, 2011 9:55 pm
by kk12345
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

Re: Reverse the Data records in a File

PostPosted: Sun Jan 23, 2011 10:11 pm
by Robert Sample
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.

Re: Reverse the Data records in a File

PostPosted: Sun Jan 23, 2011 10:14 pm
by kk12345
thank you Robert Sample.

Re: Reverse the Data records in a File

PostPosted: Mon Jan 24, 2011 10:57 am
by NicC
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.