Retrieve last 100 records from a dataset in COBOL.
Retrieve last 100 records from a dataset in COBOL.
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?
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?
Re: Retrieve last 100 records from a dataset in COBOL.
Sort the data set in reverse order then read the first 100 records.
Regards
Nic
Nic
- enrico-sorichetti
- Global Moderator
- Posts: 843
- Joined: Wed Sep 11, 2013 3:57 pm
Re: Retrieve last 100 records from a dataset in COBOL.
but then they would be in reverse orderSort the file in reverse order then read the first 100 records.

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
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

Re: Retrieve last 100 records from a dataset in COBOL.
which is why I did not suggest it! And one can always load the table from tail to head.the performance will probably suck
Regards
Nic
Nic
-
- Global Moderator
- Posts: 588
- Joined: Wed Nov 20, 2013 11:53 am
- Location: Mars
Re: Retrieve last 100 records from a dataset in COBOL.
Agree with Enrico, This looks like a homework request to me than a better way to perform a task
zprogrammer
- Anuj Dhawan
- Founder
- Posts: 2824
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: Retrieve last 100 records from a dataset in COBOL.
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.
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.

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.
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.
-
- Registered Member
- Posts: 14
- Joined: Tue May 27, 2014 8:45 pm
- Contact:
Re: Retrieve last 100 records from a dataset in COBOL.
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.
I suspect it's quite hard to position to the last record in the file and read backwards.
Martin Packer
Principal Systems Investigator, IBM
Principal Systems Investigator, IBM
- Akatsukami
- 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.
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
Re: Retrieve last 100 records from a dataset in COBOL.
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
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
Re: Retrieve last 100 records from a dataset in COBOL.
COBOL does not have arrays. It has TABLES.
Regards
Nic
Nic
Re: Retrieve last 100 records from a dataset in COBOL.
Yeah, missed to use the right terms, yes tables 
thanks
Magesh

thanks
Magesh