Hello! You wrote:
-----------------------
I created my fd
FD in the file section
fd sort-name-file
01 name-rec
03 lastname-1 pic x(30).
03 firstname-1 pic x(10).
03 lastname-2 pic x(30).
03 firstname-2 pic x(10).
03 ssn pic 9(09).
in working storage
01 sort-idx pic 9(03).
01 table-name-sort records occurs 300
indexed by sort-index
03 table-lastname-1 pic x(30)
03 lastname-1 pic x(30).
03 firstname-1 pic x(10).
03 lastname-2 pic x(30).
03 firstname-2 pic x(10).
03 ssn pic 9(09).
procedure division
set sort-index to 1.
perform display sort-fields
varying 1 by 1
until sort-idx = 20.
read input-file into table-name-sort
at end
display table-name-sort.
move firstname-1 to table-firstname-1.
lastname-1 to table-lastname-1.
firstname-2 to table-firstname-2.
lastname-2 to table-lastname-2.
ssn to table-ssn.
-----------------------------------
Is this a fixed table? There are always exactly 300 elements in it?
I will assume that your actual table size is variable i.e. not all of the 300 slots are actually being used each time. However, I won't get into variable-sized tables (the ones using DEPENDING ON clauses. We can keep this as a fixed-size table with a max of 300 occurrences.
What you can do is set up PERFORM UNTIL (or PERFORM VARYING if you prefer) paragraphs.
You want to process all of your records. For this test, I would assume that you are reading a file SORT-NAME-FILE. I'll assume that it is a sequential file.
To make things simple, let's assume that you are using a batch job and wish to display 20 records at a time, then draw a line i.e. '---------------', then display the next 20 and so forth.
Your "MAINLINE", which is your "driver" paragraph will say:
2000-MAINLINE.
MOVE +0 TO REC-CTR.
SET SORT-INDEX TO 1.
PERFORM 3000-READ-INPUT-FILE THRU 3000-EXIT
UNTIL END-OF-FILE-SW = 'Y'.
MOVE REC-CTR TO NUMBER-OF-RECS.
MOVE +0 TO REC-CTR.
SET SORT-INDEX TO 1.
PERFORM 4000-DISPLAY-RECORDS
UNTIL SORT-INDEX > NUMBER-OF-RECS.
The first sentence sets up the entire read process. You initialize your index so that when you load the sequential files to your working storage table, you will start at one. Also initialize your record counter, which will start at 0 rather than 1, because you will read the record, then count it.
You will leave the MAINLINE paragraph and go to the 3000 paragraph. There, you will read until you get to the end of the file. You will process each file, separately right after you read it i.e. you will do something with it.
In this case, you will move each record in sequence into a working storage display table, which I assume would be TABLE-NAME-SORT-RECORDS. I would give a group name to the set of fields (lastname-1, firstname-1, lastname-2, firstname-2, ssn). Call the group TABLE-REC.
So we get to paragraph: 3000-READ-INPUT-FILE.
This will say: READ INPUT-FILE
AT END
MOVE 'Y' TO END-OF-FILE-SW.
MOVE NAME-REC TO TABLE-REC (SORT-INDEX).
SET SORT-INDEX UP BY 1.
ADD +1 TO REC-CTR.
3000-EXIT.
EXIT.
And that's all you'll do with this paragraph. Get the sequential files into your working storage table, increment your index and also increment your record counter until you reach the end of your file. At the end, you will now have the number of records in REC-CTR, which you will need when doing the displays. When that happens, the END-OF-FILE-SW goes up and stops your processing. You go back to 2000-MAINLINE.
Back at 2000-MAINLINE, you will move REC-CTR to NUMBER-OF-RECS. Then you will re-initialize REC-CTR to 0 so you can use it to keep track of when you have gotten to 20 so you can then place the '------------' in between the groups of 20. You will also re-initialize your index (to 1) for the display of your records. Again, you want to start at the beginning of your table. And your MAINLINE sentence will simply state: PERFORM 4000-DISPLAY-RECORDS UNTIL SORT-INDEX > NUMBER-OF-RECS.
In the paragraph 4000-DISPLAY-RECORDS, it should be coded like this:
4000-DISPLAY-RECORDS.
DISPLAY TABLE-REC (SORT-INDEX).
ADD +1 TO REC-CTR.
DIVIDE 20 INTO REC-CTR GIVING REC-RESULT
REMAINDER EVERY-20-FLAG.
What you are doing is looking to see when the REC-CTR has reach either 20 or a multiple of 20. You don't care about the result, but you care about the remainder (a COBOL reserved word). If this remainder = 0, then we are at 20 or a multiple of 20, and you will display a '-------------' at this point. So the remainder is a sort of "flag" telling the program to separate the records into groups of 20.
So the next line of code will be:
IF EVERY-20-FLAG = 0
DISPLAY '--------------------'
END-IF.
If you want, you can do something other than display a line. You can write it to another record, move the set of records off somewhere else or whatever. It's a sort of "control-break."
When you reach NUMBER-OF-RECS, the processing will stop.
Hope this helps.
Nina Too