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

Display the 3rd record from the last of a sequential file


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

New User


Joined: 17 May 2006
Posts: 17

PostPosted: Fri May 19, 2006 2:14 pm
Reply with quote

How to display the 3rd record from the last of a sequential file (not know the no. of records). (Thru Cobol Prog)
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Sat May 20, 2006 4:41 am
Reply with quote

bcmohanty,

I think you?re going to have to create a Cobol table occurs 3 times and every time you read a record, push the entries in the table down and add the new record at the top. When an EOF occurs the (3) entry will be you?re third from last.

Code:


 FD INPUT-FILE
 01  INPUT-RECORD      PIC X(80).

 WORKING-STORAGE.

 01  INPUT-TABLE.
     05  INPUT-REC     PIC X(80)   OCCURS 3 TIMES.

 PROCEDURE-DIVISION.

     PERFORM READ-INPUT-RECORD.

     PERFORM
       UNTIL INPUT-RECORD-EOF
         MOVE INPUT-REC(2)        TO INPUT-REC(3)
         MOVE INPUT-REC(1)        TO INPUT-REC(2)
         MOVE INPUT-RECORD        TO INPUT-REC(1)
         PERFORM READ-INPUT-RECORD
     END-PERFORM.

     DISPLAY INPUT-REC(3).



Dave
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun May 21, 2006 10:17 am
Reply with quote

If this is more than just an intellectual exercise, I'd opt for using the FileAid COPYBACK feature, then accessing the rec from this output.
Code:

//COPYREC  EXEC PGM=FILEAID
//DD01     DD  DSN=input.dsn,DISP=SHR
//DD01O    DD  DSN=output.dsn,DISP=SHR
//SYSIN    DD  *
$$DD01 COPYBACK SELECT=3,OUT=1
//SYSPRINT DD  SYSOUT=A
//*
//NEXT    EXEC PGM=yrpgm
//HIGHREC DD DSN=file.contains.3rd.high.rec
//etc.... 

You can use CB in place of COPYBACK. COPYBACK works for VSAM and seq files, but not GDGs.
Back to top
View user's profile Send private message
hikaps14

Active User


Joined: 02 Sep 2005
Posts: 189
Location: Noida

PostPosted: Mon May 22, 2006 11:20 am
Reply with quote

Hi,

According to me just do a sort on file in reverse order .
then read the file three times .

i hope u get the approach .

Thanks,
-Kapil .
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Mon May 22, 2006 10:48 pm
Reply with quote

The question was:

How to display the 3rd record from the last of a sequential file (not know the no. of records). (Thru Cobol Prog)

Dave
Back to top
View user's profile Send private message
hikaps14

Active User


Joined: 02 Sep 2005
Posts: 189
Location: Noida

PostPosted: Mon May 22, 2006 11:42 pm
Reply with quote

Hi,

wel i think u misunderstood a bit .

wat i want to do is just sort the file in reverse order.

this will bring the third last record in file to no. 3 p[osition from top .

n now we can easily read n get the third record from top .

i hope i got the prob. correctly .

or is it completely diff. prob.

plz do tell me if i am wrong .


-Thanks,
Kapil .
Back to top
View user's profile Send private message
vijayamadhuri

Active User


Joined: 06 Apr 2005
Posts: 180

PostPosted: Tue May 23, 2006 12:04 am
Reply with quote

Quote:
How to display the 3rd record from the last of a sequential file (not know the no. of records). (Thru Cobol Prog)


using Sort reverse the file and use this file as i/p . increment the counter as it reads each record and u would print only if it the third record
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Wed May 24, 2006 5:54 am
Reply with quote

The problem with these solutions is reading the entire file to "display" the rec.
The FileAid solution uses an Assembler reverse read. If this were a real world problem Assembler or some more direct access to the record would be the preferred approach.
Back to top
View user's profile Send private message
gskulkarni

New User


Joined: 01 Mar 2006
Posts: 70

PostPosted: Wed May 24, 2006 3:00 pm
Reply with quote

mmwife wrote:
The problem with these solutions is reading the entire file to "display" the rec.


However, in the first post (s)he has mentioned [quote="bcmohanty"](Thru Cobol Prog)[quote="bcmohanty"]
So,
If the file is in specific sort order: I guess people have suggested a very good option of sorting the file in reverse order and reading the first 3 recs. 3rd one can be displayed.

Otherwise, if the file is unsorted and records are not in a particular sort order:

1) If you have to use cobol program, then you can do a IDCAMS COUNT and store the result in a dataset. In your COBOL prog read that dataset and get the count-4. Skip as many records and display your record number 3 from 'down'!

2) Fileaid solution serves the purpose right. (This is the best one!!)



So if the file is sorted
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Thu May 25, 2006 3:57 am
Reply with quote

If the file is small then I guess it's OK to read the file, but sorting the file, from a performance perspective, is the worst solution regardless of the file's size.

I don't know why this kind of thing would have to be done; it sounds like faulty system design.
Back to top
View user's profile Send private message
sharda

New User


Joined: 13 Sep 2006
Posts: 7

PostPosted: Tue Oct 17, 2006 5:07 pm
Reply with quote

Hi Davidatk,
I reffer ur following code but as I am fresher in this field, I am not able to get the following code exactly . Can u plz explain the code in detail (from beginning to end ) .
FD INPUT-FILE
01 INPUT-RECORD PIC X(80).

WORKING-STORAGE.

01 INPUT-TABLE.
05 INPUT-REC PIC X(80) OCCURS 3 TIMES.

PROCEDURE-DIVISION.

PERFORM READ-INPUT-RECORD.

PERFORM
UNTIL INPUT-RECORD-EOF
MOVE INPUT-REC(2) TO INPUT-REC(3)
MOVE INPUT-REC(1) TO INPUT-REC(2)
MOVE INPUT-RECORD TO INPUT-REC(1)
PERFORM READ-INPUT-RECORD
END-PERFORM.

DISPLAY INPUT-REC(3).


Thks
Sharda
Back to top
View user's profile Send private message
prashanth1

New User


Joined: 27 Sep 2006
Posts: 47
Location: Hyderabad

PostPosted: Wed Oct 18, 2006 1:55 pm
Reply with quote

Hi,

We can achieve this by reading the file in reverse order

Syntax : OPEN INPUT file-name REVERSED

Here it opens the file in reverse direction

Just put one counter variable , when the counter variable becomes 3, just write that record into output file.


Pls let me know , If I wrng icon_smile.gif
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Thu Oct 19, 2006 2:58 pm
Reply with quote

OPEN INPUT file-name REVERSED

plz let me now if this statement can be used for all type of files(disk/tape)


regards

sanooja [/quote]
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Thu Oct 19, 2006 7:32 pm
Reply with quote

This can be used only on tape files, and I believe it's going to be obsolete in the next Cobol release

Dave
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 How to split large record length file... DFSORT/ICETOOL 7
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Access to non cataloged VSAM file JCL & VSAM 18
No new posts FINDREP - Only first record from give... DFSORT/ICETOOL 3
Search our Forums:

Back to Top