Retrieve last 100 records from a dataset in COBOL.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Previous topicNext topic

Topic Author
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 » Thu Sep 08, 2016 4:47 pm

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: 578
Joined: Wed Apr 23, 2014 8:45 pm

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

Post by nicc » Thu Sep 08, 2016 5:28 pm

Sort the data set in reverse order then read the first 100 records.


Regards
Nic


enrico-sorichetti
Global Moderator
Global Moderator
Posts: 869
Joined: Wed Sep 11, 2013 3:57 pm

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

Post by enrico-sorichetti » Thu Sep 08, 2016 5:34 pm

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: 578
Joined: Wed Apr 23, 2014 8:45 pm

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

Post by nicc » Thu Sep 08, 2016 5:36 pm

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: 603
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

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

Post by zprogrammer » Thu Sep 08, 2016 7:51 pm

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: 2446
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

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

Post by Anuj Dhawan » Fri Sep 09, 2016 2:05 pm

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


MartinPacker
Registered Member
Posts: 11
Joined: Tue May 27, 2014 8:45 pm

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

Post by MartinPacker » Fri Sep 09, 2016 2:39 pm

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: 117
Joined: Tue Oct 20, 2015 3:20 am
Location: Bloomington, IL

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

Post by Akatsukami » Fri Sep 09, 2016 7:40 pm

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
Zodiac: Gemini

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

Post by Magesh_j » Sun Sep 11, 2016 1:13 am

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: 578
Joined: Wed Apr 23, 2014 8:45 pm

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

Post by nicc » Sun Sep 11, 2016 3:27 pm

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
Zodiac: Gemini

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

Post by Magesh_j » Mon Sep 12, 2016 10:57 am

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

thanks
Magesh



Previous topicNext topic

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