Retrieve last 100 records from a dataset in COBOL.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Blessings
New Member
Posts: 1
Joined: Mon Feb 02, 2015 4:33 pm

Retrieve last 100 records from a dataset in COBOL.

Post by Blessings »

Hi,

How can we retrieve last 100 records in COBOL, from a dataset. I know we can do it in SORT but how can we do it in COBOL?
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by nicc »

Sort the data set in reverse order then read the first 100 records.
Regards
Nic
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 824
Joined: Wed Sep 11, 2013 3:57 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by enrico-sorichetti »

Sort the file in reverse order then read the first 100 records.
but then they would be in reverse order 8-)

the logic is that of a sliding window...
build an array of 100 records,
read the rest one at the time,
shift the array by one and insert the just read record in the last positon.

at EOF the array will contain the last 100 record
the performance will probably suck, but You satisfied the requirement of doing it Yourself
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by nicc »

the performance will probably suck
which is why I did not suggest it! And one can always load the table from tail to head.
Regards
Nic
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Retrieve last 100 records from a dataset in COBOL.

Post by zprogrammer »

Agree with Enrico, This looks like a homework request to me than a better way to perform a task
zprogrammer
User avatar
Anuj Dhawan
Founder
Posts: 2799
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Retrieve last 100 records from a dataset in COBOL.

Post by Anuj Dhawan »

Enterprise COBOL allows to read the files in reverse order only if they are single-reel and are FB.

If you can choose ASSEMBLER, then you might use BSP macro even for DASD files.

IF <none of the above is a practical choice>
GO TO
<Post from Enrico >
END-IF.

PS. It's advisable not to use GO TOs in your COBOL programs. :D
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
MartinPacker
Registered Member
Posts: 14
Joined: Tue May 27, 2014 8:45 pm
Contact:

Re: Retrieve last 100 records from a dataset in COBOL.

Post by MartinPacker »

If there is a serious point, it is this:

I suspect it's quite hard to position to the last record in the file and read backwards.
Martin Packer
Principal Systems Investigator, IBM
User avatar
Akatsukami
Global Moderator
Global Moderator
Posts: 122
Joined: Tue Oct 20, 2015 3:20 am
Location: Bloomington, IL
Contact:

Re: Retrieve last 100 records from a dataset in COBOL.

Post by Akatsukami »

For a file, yes. For a data set, not so much (although it's still a non-trivial effort).
"I come to the conclusion that, men loving according to their own will and fearing according to that of the prince, a wise prince should establish himself on that which is in his own control and not in that of others." -- Niccolò Machiavelli
User avatar
Magesh_j
Registered Member
Posts: 33
Joined: Sun Sep 04, 2016 8:50 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by Magesh_j »

Have two arays of 100 say array1 and array2
1.Clear array1 and load 100 record
2.after 100 record clear array2 load 100

Perform step1 and stpe2 as a loop untIl you reach eof

Once you reach eof, used the last index value fetch record from other index

You will know from which index you need to move from.

Example 340 records are there

Load
array 1 100
Arrar 2 100
Array 1 100
Array 2 40

You need to track what is the last index and whether it is array1 or array2

Now last index is 40 and last array is array2

So now you know, you need to fetch from 41St to 100 in array1 and 1 to 40 from array2.


Thanks
Magesh
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by nicc »

COBOL does not have arrays. It has TABLES.
Regards
Nic
User avatar
Magesh_j
Registered Member
Posts: 33
Joined: Sun Sep 04, 2016 8:50 pm

Re: Retrieve last 100 records from a dataset in COBOL.

Post by Magesh_j »

Yeah, missed to use the right terms, yes tables :)

thanks
Magesh
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”