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

Option to pick last 5 records from flat file


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
hamsalakshmi

New User


Joined: 30 Aug 2010
Posts: 3
Location: India

PostPosted: Mon Sep 20, 2010 1:48 pm
Reply with quote

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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Sep 20, 2010 2:48 pm
Reply with quote

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

New User


Joined: 26 Sep 2007
Posts: 28
Location: Bangalore

PostPosted: Mon Sep 20, 2010 3:12 pm
Reply with quote

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

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Sep 20, 2010 3:20 pm
Reply with quote

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

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Sep 20, 2010 5:16 pm
Reply with quote

But why use a feature that is not yet obsolete, when one might expect it to become obsolete at some point?
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Sep 20, 2010 5:28 pm
Reply with quote

Actually, if I were to do what is asked - I could have used what Enrico has suggested. BUT...
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Sep 20, 2010 5:33 pm
Reply with quote

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

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Sep 20, 2010 5:35 pm
Reply with quote

Well, agreed. I gave just another altervative...
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Mon Sep 20, 2010 5:54 pm
Reply with quote

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

New User


Joined: 30 Aug 2010
Posts: 3
Location: India

PostPosted: Tue Sep 21, 2010 1:15 pm
Reply with quote

Thanks to all for your suggestions........
Back to top
View user's profile Send private message
bhavin.mehta

New User


Joined: 25 Jun 2012
Posts: 34
Location: India

PostPosted: Mon Jun 24, 2013 7:36 pm
Reply with quote

@dbzTHEdinosauer

I really like the way you code... icon_biggrin.gif
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 -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts FTP VB File from Mainframe retaining ... JCL & VSAM 8
No new posts Extract the file name from another fi... DFSORT/ICETOOL 6
No new posts How to split large record length file... DFSORT/ICETOOL 10
Search our Forums:

Back to Top