View previous topic :: View next topic
|
Author |
Message |
mighty
New User
Joined: 21 May 2008 Posts: 26 Location: chennai
|
|
|
|
I have an input file with n number of records.
I have to write the 3rd record from the last to my output file using cobol.
Can anyone explain me how to achieve this. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
since you have to write the 3rd record from the last:
3rd
2rd
1st
last
eof
I would read and save records in a 4 item table.
open input
perform varying index from 1 to 4
read
move record to item(index)
end-perform
read
perform until eof
move items 2/3/4 to 1/2/3 (i would make it a group move)
move record to item(4)
read
end-perform
close input
open output
write item(1)
close output
goback.
this logic obviously assums you have 4 or more records.
if it turns out that we are basing on 3 records, modify the logic accordingly |
|
Back to top |
|
|
mighty
New User
Joined: 21 May 2008 Posts: 26 Location: chennai
|
|
|
|
can you explain me this in detail
"move items 2/3/4 to 1/2/3 (i would make it a group move) "
how to move records like this
for example my input file layout
empid name
1 a
2 b
3 c
4 d
5 e
Thanks for your quick response |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
In your example data, which record do you consider "the 3rd record from the last"? |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2547 Location: Silicon Valley
|
|
|
|
You have an array, initialized to blanks. You have a loop which reads a record. Each time you read, you move the old ones up 1 and add the new record at the end.
Code: |
init pass 1 pass 2 pass 3 pass 4 ... pass 531 pass 532
1 empty 1 empty 1 empty 1 empty 1 rec 1 1 rec528 1 rec529
2 empty 2 empty 2 empty 2 rec 1 2 rec 2 2 rec529 2 rec530
3 empty 3 empty 3 rec 1 3 rec 2 3 rec 3 3 rec530 3 rec531
4 empty 4 rec 1 4 rec 2 4 rec 3 4 rec 4 4 rec531 4 rec532
|
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Yup, the only question now is which does TS consider the "3rd from the last". . .
d |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
What Dick meant by group move is like this:
Code: |
01 last-three-recs.
05 last-rec pic x(80).
05 second-from-last-rec pic x(80).
05 third-from-last-rec pic x(80).
01 redefines last-three-recs.
05 last-two-recs pic x(160).
05 pic x(80).
01 redefines last-three-recs.
05 pic x(80).
05 shift-em-down-one pic x(160). |
move last-two-recs to shift-em-down-one,
read your-file into last-rec,
repeat the above move and read until end of file at which time third-from-last-rec is the one you want. The above assumes a LRECL of 80. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
I've asked for edit authority on my own posts but haven't gotten it yet. I would have aligned the pic clauses. Also, Dick mentioned group move although mine are elementary moves but I believe it's the functionality that he was alluding to. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hi Terry,
Quote: |
I've asked for edit authority on my own posts but haven't gotten it yet. |
Even moderators cannot edit all of their own posts. . .
Quote: |
I would have aligned the pic clauses. |
Using "Preview" lets you see your post the way the forum will see it rather than the way it looks in the reply editor. While it doesn't help after the submit, it can help "up front".
fwiw. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Thanks Dick. I didn't realize that editing wasn't allowed. Some bulletin boards allow editing after you've shown that you won't abuse the privilege! I should have thought of the Preview option before submitting. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
You're welcome
d |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Hi,
Terry Heinze wrote: |
I've asked for edit authority on my own posts but haven't gotten it yet. |
Sometimes, when editing is quite necessary I request some Moderator via PM, they had been pretty helpful so far.. |
|
Back to top |
|
|
mighty
New User
Joined: 21 May 2008 Posts: 26 Location: chennai
|
|
|
|
Thanks all for your help.The answer for my question got clarified from your posts |
|
Back to top |
|
|
|