Page 1 of 1

Retrieve last 100 records from a dataset in COBOL.

Posted: Thu Sep 08, 2016 4:47 pm
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?

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

Posted: Thu Sep 08, 2016 5:28 pm
by nicc
Sort the data set in reverse order then read the first 100 records.

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

Posted: Thu Sep 08, 2016 5:34 pm
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

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

Posted: Thu Sep 08, 2016 5:36 pm
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.

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

Posted: Thu Sep 08, 2016 7:51 pm
by zprogrammer
Agree with Enrico, This looks like a homework request to me than a better way to perform a task

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

Posted: Fri Sep 09, 2016 2:05 pm
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

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

Posted: Fri Sep 09, 2016 2:39 pm
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.

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

Posted: Fri Sep 09, 2016 7:40 pm
by Akatsukami
For a file, yes. For a data set, not so much (although it's still a non-trivial effort).

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

Posted: Sun Sep 11, 2016 1:13 am
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

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

Posted: Sun Sep 11, 2016 3:27 pm
by nicc
COBOL does not have arrays. It has TABLES.

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

Posted: Mon Sep 12, 2016 10:57 am
by Magesh_j
Yeah, missed to use the right terms, yes tables :)

thanks
Magesh