View previous topic :: View next topic
|
Author |
Message |
bcmohanty
New User
Joined: 17 May 2006 Posts: 17
|
|
|
|
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 |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
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 |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
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 |
|
|
hikaps14
Active User
Joined: 02 Sep 2005 Posts: 189 Location: Noida
|
|
|
|
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 |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
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 |
|
|
hikaps14
Active User
Joined: 02 Sep 2005 Posts: 189 Location: Noida
|
|
|
|
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 |
|
|
vijayamadhuri
Active User
Joined: 06 Apr 2005 Posts: 180
|
|
|
|
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 |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
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 |
|
|
gskulkarni
New User
Joined: 01 Mar 2006 Posts: 70
|
|
|
|
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 |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
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 |
|
|
sharda
New User
Joined: 13 Sep 2006 Posts: 7
|
|
|
|
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 |
|
|
prashanth1
New User
Joined: 27 Sep 2006 Posts: 47 Location: Hyderabad
|
|
|
|
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 |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
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 |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
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 |
|
|
|