Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Option to pick last 5 records from flat file

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Option to pick last 5 records from flat file
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Mon Sep 20, 2010 2:48 pm    Post subject: Reply to: Option to pick last 5 records from falt file.
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    Post subject:
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

Senior Member


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

PostPosted: Mon Sep 20, 2010 3:20 pm    Post subject:
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

Active Member


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

PostPosted: Mon Sep 20, 2010 5:16 pm    Post subject:
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

Senior Member


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

PostPosted: Mon Sep 20, 2010 5:28 pm    Post subject:
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

Active Member


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

PostPosted: Mon Sep 20, 2010 5:33 pm    Post subject:
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

Senior Member


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

PostPosted: Mon Sep 20, 2010 5:35 pm    Post subject:
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    Post subject:
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    Post subject: Reply to: Option to pick last 5 records from flat file.
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: 32
Location: India

PostPosted: Mon Jun 24, 2013 7:36 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Add PD field from 2nd file to PD in 1st Sushant Garje DFSORT/ICETOOL 6 Thu Dec 01, 2016 4:32 pm
No new posts File Aid to File Manager conversion murali3955 IBM Tools 4 Thu Nov 24, 2016 3:41 pm
No new posts Limit duplicate records in the SORT pshongal SYNCSORT 6 Mon Nov 21, 2016 12:54 pm
No new posts How to convert the VBM file to VB or... Sulabh Agrawal JCL & VSAM 4 Fri Nov 18, 2016 1:04 pm
No new posts CICS Roll back partially - Need to re... dwijadas CICS 4 Wed Nov 16, 2016 4:30 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us