Be generous in your table declarations -- if you guess small, you'll have to rerun the compile and link (bind) before rerunning the program; guessing too big merely means your program has unused memory. And depending upon the version of COBOL you have anywhere from 128 MB (version 3.4) to 1 GB (version 6.1 - technically, 999,999,999 bytes is not quite 1 GB) of memory available, so define your table something like
Code: Select all
01 HOLD-TABLE.
05 HT-ENTRY OCCURS 5000
INDEXED BY HT-INDEX
HT-WRITE-INDEX
PIC X(2024).
Read the first record and move it to the first table entry, setting HT-INDEX to 1. After that, start your read loop. In the read loop, if the record is NOT a header, add 1 to HT-INDEX (SET HT-INDEX UP BY 1) and move the input data to HT-ENTRY (HT-INDEX), then read the next record. If the record IS a header, write the table out (see below), set HT-INDEX to 1, and move the record to HT-ENTRY (HT-INDEX). When you hit end-of-file, you'll need to write out the table because the last group of records in the file is still in the table. I did not include the code to validate that you don't run out of table entries, but you'll want to check for that since you don't know how many records can occur for a header.
Your code to write the table will be something like
Code: Select all
PERFORM
VARYING HT-WRITE-INDEX FROM 1 BY 1
UNTIL HT-WRITE-INDEX > HT-INDEX
WRITE OUT-FILE-RECORD FROM HT-ENTRY (HT-WRITE-INDEX)
END-PERFORM
If you use index as I show, you'll need to use SET instead of ADD / MOVE (which are used for subscripts).
I would count the input records, the input headers, the maximum number of records for a header, the output records and print these numbers out at the end of the program to get a better idea of what the data set has.