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

file manipulation using cobol


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
mighty

New User


Joined: 21 May 2008
Posts: 26
Location: chennai

PostPosted: Fri Aug 15, 2008 9:41 pm
Reply with quote

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

Global Moderator


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

PostPosted: Fri Aug 15, 2008 10:03 pm
Reply with quote

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

New User


Joined: 21 May 2008
Posts: 26
Location: chennai

PostPosted: Fri Aug 15, 2008 10:19 pm
Reply with quote

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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Aug 16, 2008 12:06 am
Reply with quote

Hello,

In your example data, which record do you consider "the 3rd record from the last"?
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2546
Location: Silicon Valley

PostPosted: Sat Aug 16, 2008 1:32 am
Reply with quote

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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Aug 16, 2008 1:46 am
Reply with quote

Yup, the only question now is which does TS consider the "3rd from the last". . .

d
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat Aug 16, 2008 10:43 am
Reply with quote

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

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat Aug 16, 2008 11:00 am
Reply with quote

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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Aug 16, 2008 8:48 pm
Reply with quote

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

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sun Aug 17, 2008 8:26 am
Reply with quote

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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sun Aug 17, 2008 8:32 am
Reply with quote

You're welcome icon_smile.gif

d
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 Aug 18, 2008 11:17 am
Reply with quote

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.. icon_smile.gif
Back to top
View user's profile Send private message
mighty

New User


Joined: 21 May 2008
Posts: 26
Location: chennai

PostPosted: Mon Aug 18, 2008 10:31 pm
Reply with quote

Thanks all for your help.The answer for my question got clarified from your posts
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 -> Mainframe Interview Questions

 


Similar Topics
Topic Forum Replies
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
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
Search our Forums:

Back to Top