Page 1 of 1

how to write cobol to sort in a table

PostPosted: Thu Apr 21, 2011 11:11 am
by cp_ur530
hello,
I have read the record in the file into a table , but the recrods are not in sequence on the key ,such as
01 record-table.
05 cust-mast-record occurs 500 times
ascending key is branch-number
indexed by cm-index.
10 branch-number pic 99.
10 salerep-number pic 99.


how to sort the table?

Re: how to write cobol to sort in a table

PostPosted: Thu Apr 21, 2011 11:17 am
by NicC
First you search the forum - someone else has asked this question within the last week. It is, presumably, a homework question as your table has exactly the same structure.

Re: how to write cobol to sort in a table

PostPosted: Thu Apr 21, 2011 12:14 pm
by BillyBoyo
cp_ur530 wrote:I have read the record in the file into a table , but the recrods are not in sequence on the key


Is there any reason you can't sort the input file into this order (with your system SORT as a seperate STEP/JOB), then store in your table?

If there is a reason, you might still be lucky.

You have branch-number and salerep-number. You have defined them as numeric, I don't know why since you are never going to do a calculation with them - so better as PIC XX.

Irrespective of that, if they genuninely are numeric:

Define a table of 10,000 items, let's do 10,000 flags, you'll know if you need to include more data.

Set all the flags off at before you read any records.

Make a number out of your branch/salesperson. Start off as usage Display. Then move the resulting number to a COMP PIC S9(4), because you are going to use it as a subscript.

Add one to the value of this subscript - this is to deal with the case 0000 if it can exist. If it can't logically exist, test for it in your program and behave appropriately.

Then, using the subscript you have created, set the appropriate flag in the table "on".

You can then interrogate the table as you need, by using the same technicque.

Re: how to write cobol to sort in a table

PostPosted: Sat Apr 23, 2011 12:30 pm
by cp_ur530
NicC wrote:First you search the forum - someone else has asked this question within the last week. It is, presumably, a homework question as your table has exactly the same structure.


I had read that question and its replay ,but i think i didn't find the answer i want , i know i can write the sort algorithm by myself ,but i still want to know how to use "sort" in the COBOL program to sort in a table just like sort a file

Re: how to write cobol to sort in a table

PostPosted: Sat Apr 23, 2011 12:38 pm
by cp_ur530
BillyBoyo wrote:Is there any reason you can't sort the input file into this order (with your system SORT as a seperate STEP/JOB), then store in your table?


read the records that not in sequence into a table ,just want to make it easy ,not really like that ,
the records are not in sequence ,because I add some records to the table from other file

Re: how to write cobol to sort in a table

PostPosted: Sat Apr 23, 2011 12:48 pm
by BillyBoyo
So, what about the suggested solution, which is to use a "sparsely populated" table and a subscript generated from your two key items.

If you want to know more than just the "existence", just include whatever it is in the table.

If you want the table "sorted", after you have loaded all the data, go through the table ignoring any which are not set, and moving each that is set to the highest available slot. But no real need. The table will work fine as long as you have the keys.

Re: how to write cobol to sort in a table

PostPosted: Sat Apr 23, 2011 6:11 pm
by Robert Sample
but i still want to know how to use "sort" in the COBOL program to sort in a table just like sort a file
The SORT verb requires using a file -- the sort file. So you could load the table, use SORT where the INPUT PROCEDURE moves the table entries to the SORT record and releases it, while the OUTPUT PROCEDURE reloads the table. If you expect to use SORT USING GIVING, then your expectation cannot be met, period. USING and GIVING only work with files.