View previous topic :: View next topic
|
Author |
Message |
hamsalakshmi
New User
Joined: 30 Aug 2010 Posts: 3 Location: India
|
|
|
|
Hi,
Ineed to get last 5 records from a flat file and the output file should have that 5 records.
For this scenario i need to code a COBOL program.
Do we have the option to get last 5 records from a flat file? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
do You have a doctor' s prescription to be forced to use COBOL ???
search the <sort> forums for samples on how to do it
anyway here is a snippet to be tested
Code: |
//S1 EXEC PGM=ICETOOL
//SYSOUT DD SYSOUT=*
//DFSMSG DD SYSOUT=*
//TOOLMSG DD SYSOUT=*
//IN DD DSN=YOUR INPUT HERE
//OUT DD DSN=YOUR OUTPUT HERE
//TOOLIN DD *
SUBSET FROM(IN) TO(OUT) KEEP INPUT LAST(<number of records to be kept>)
/*
|
|
|
Back to top |
|
|
nareshkp
New User
Joined: 26 Sep 2007 Posts: 28 Location: Bangalore
|
|
|
|
If you are forced to use a COBOL program, you can consider the following approach:
1. Define a working storage table for 5 occurences with input record format.
2. Read the input file into the WS-table. Over-write from the first occurence when the WS-table is filled up.
3. At the end of the input file, you will have the last 5 records in WS-table. Write them into output file.
But remember, the 5 records in the output file may or may not be in the same order. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Well, the requirements!
Do you have file on Tape? If yes, look at REVERSE verb in COBOL Manual.
If tape-file is multi-volume - REVERSE might or might not work. But just for five records I'd, probably, ignore the fact of file being multi-volume. (It's a dangerous and not-so-technical assumption tough; but OP has not talked about the approx. records on file - so I'll take the benefit of doubt.)
It should be noted that manual says:
Quote: |
REVERSED
Valid only for sequential single-reel files. REVERSED is not valid for VSAM files.
If the concept of reels has no meaning for the storage medium (for example, a direct access device), the REVERSED and NO REWIND phrases do not apply.
NO REWIND
Valid only for sequential single-reel files. It is not valid for VSAM files. |
Though OPEN REVERSED is obsolete according to the COBOL standards committee. The option has not been made obsolete by IBM in its COBOL. so, possibly you stand a chance. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
But why use a feature that is not yet obsolete, when one might expect it to become obsolete at some point? |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Actually, if I were to do what is asked - I could have used what Enrico has suggested. BUT... |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
nareshkp's COBOL solution is fine, given the COBOL requirement.
But his caution about the record order shold be discarded in favor of writing the final 5 records out in the order in which they were received.
This is not too hard - just fill the table circularly and keep track of which position was last filled, then start writing from the following position, counting with mod5 arithmetic. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Well, agreed. I gave just another altervative... |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
this gets the job done without worrying about were/when/what
Code: |
01 last-six-area.
05 first-five pic x(400).
05 last-read pic x(080).
01 last-six-hold-area
redefines
last-six-area.
05 filler pic x(080).
05 last-five pic x(400).
01 last-six-records
redefines
last-six-area.
05 one-of-the-records pic x(080) occurs 6 times.
INITIALIZE last-six-area
OPEN input-file
Perform read-input
until eof
CLOSE input-file
DISPLAY 'fourth from last ' one-of-the-records(1)
DISPLAY 'third from last ' one-of-the-records(2)
DISPLAY 'second from last ' one-of-the-records(3)
DISPLAY 'first from last ' one-of-the-records(4)
DISPLAY 'last ' one-of-the-records(5)
GOBACK
READ-INPUT.
move last-five to first-five
Read input-file-record into last-read
.
999-exit.
exit.
|
|
|
Back to top |
|
|
hamsalakshmi
New User
Joined: 30 Aug 2010 Posts: 3 Location: India
|
|
|
|
Thanks to all for your suggestions........ |
|
Back to top |
|
|
bhavin.mehta
New User
Joined: 25 Jun 2012 Posts: 34 Location: India
|
|
|
|
@dbzTHEdinosauer
I really like the way you code... |
|
Back to top |
|
|
|