0

the format of input file is like this:

Region  ******* Company Name  
  A               
  B            
  C            
  A          
  C      

with many lines.
I need to get a output file to rearrange the file with headers like this:

Company in Region A:  
name  
name  
name...  

Company in Region B:  
name  
name  
name..  

Company in Region C:  
name  
name  
name..  

My question is because the region in input file is not ordered. How can I add the second Region A company back in the header "Company in Region A"? I can only read the file one time (I cannot first all do lines with region A then reopen the file to read again). And I can only have 1 output file.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
FrankT
  • 1
  • 2
  • Is this homework? If not, what is the reason for the limitations you specify? You should really strongly not consider at all opening and closing a file multiple times, even when you are "allowed" to. For homework, Bruce has the answer (SORT) though with a tragic example from HP/Compaq :-) so *you* get to do a nice one. If not homework, describe your limitations more fully. – Bill Woodger Oct 30 '14 at 17:18
  • This comment is informational, and only tangential to the question. As you tagged this as opencobol, check out the reportwriter branch on SourceForge. The Report Writer module is a powerful module. – Brian Tiffin Jan 15 '15 at 08:25

3 Answers3

3

You could use the Sort verb with input/output procedure to sort the file into Region Sequence.

You can find many examples in Google. This ShorExample has a short Sort example, there is more info here You will probably need both input and output procedures

Sort Example:

PROCEDURE DIVISION. 
000-SORT SECTION. 
010-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1 
                   ON DESCENDING KEY SORT-KEY-2 
                   USING INPUT-FILE 
                   OUTPUT PROCEDURE IS 200-WRITE-OUTPUT 
                                  THRU 230-DONE-OUTPUT. 
    DISPLAY "END OF SORT". 
    STOP RUN. 
200-WRITE-OUTPUT SECTION. 
210-OPEN-OUTPUT. 
    OPEN OUTPUT OUTPUT-FILE. 
220-GET-SORTED-RECORDS. 
    RETURN SORT-FILE AT END 
        CLOSE OUTPUT-FILE 
        GO TO 230-DONE-OUTPUT. 
    MOVE SORT-RECORD TO OUTPUT-RECORD. 
    WRITE OUTPUT-RECORD. 
    GO TO 220-GET-SORTED-RECORDS. 
230-DONE-OUTPUT SECTION. 
240-EXIT-OUTPUT. 
    EXIT. 
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
1

If you are doing homework, remember that the goal of homework is not to solve a problem, it is to demonstrate that you have learned the class material. So, if this is homework, create your own solution based on what you have been taught. If you are trying to solve a real-life problem, the example below might help point you in the right direction. If you have not been taught sorting using an output procedure, you do not want to use the example below to do your homework. That said, the following program works using the sample data shown with GNUCobol. Notice in particular how paragraph OUTPUT-CO-BY-REGION-REPORT is used by the SORT.

--- contents of sample data file COMPANY.DAT ---

A WAL-MART
B EXXON
C CHEVRON
B BERKSHIRE
A APPLE
C GENERAL MOTORS

   IDENTIFICATION DIVISION.
   PROGRAM-ID. COMPANY-BY-REGION.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

   SELECT COMPANY-FILE
       ASSIGN TO 'COMPANY.DAT'
       ORGANIZATION IS LINE SEQUENTIAL.

   SELECT COMPANY-SORT-FILE
       ASSIGN TO DISK.

   SELECT REGION-REPORT-FILE
       ASSIGN TO 'COMPANY-BY-REGION.RPT'
       ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.

   FD  COMPANY-FILE.
   01  COMPANY-RECORD.
       02  COM-REGION                   PIC X.
       02  FILLER                       PIC X.
       02  COM-NAME                     PIC X(20).

   SD  COMPANY-SORT-FILE.
   01  COMPANY-SORT-RECORD.
       02  SORT-REGION                  PIC X.
       02  FILLER                       PIC X.
       02  SORT-NAME                    PIC X(20).

   FD  REGION-REPORT-FILE.
   01  REGION-REPORT-RECORD             PIC X(20).

   WORKING-STORAGE SECTION.

   01  SORTED-DATA-REMAINS              PIC X VALUE 'Y'.
       88  NO-SORTED-DATA-REMAINS       VALUE 'N'.

   01  WS-SORTED-RECORD.
       02  WS-REGION                    PIC X.
       02  FILLER                       PIC X.
       02  WS-NAME                      PIC X(20).

   01  REGION-REPORT-HEADER.
       02  FILLER PIC X(18) VALUE 'COMPANY IN REGION '.
       02  HEAD-REGION                  PIC X VALUE SPACE.
       02  FILLER                       PIC X VALUE ':'.

   01  REGION-DETAIL.
       02  DET-NAME                     PIC X(20).

   01  PRIOR-REGION                     PIC X.

   PROCEDURE DIVISION.
   WRITE-COMPANY-BY-REGION-REPORT.
       SORT COMPANY-SORT-FILE
           ASCENDING KEY SORT-REGION
                         SORT-NAME
           USING COMPANY-FILE
           OUTPUT PROCEDURE OUTPUT-CO-BY-REGION-REPORT
       STOP RUN
       .

   OUTPUT-CO-BY-REGION-REPORT.
       OPEN OUTPUT REGION-REPORT-FILE
       PERFORM UNTIL NO-SORTED-DATA-REMAINS
           RETURN COMPANY-SORT-FILE INTO WS-SORTED-RECORD 
               AT END SET NO-SORTED-DATA-REMAINS TO TRUE
               NOT AT END
                   PERFORM WRITE-COMPANY-RECORD
       END-PERFORM
       CLOSE REGION-REPORT-FILE
       .

   WRITE-COMPANY-RECORD.
       IF HEAD-REGION = SPACE 
       OR WS-REGION NOT = PRIOR-REGION
           PERFORM PRINT-HEADER
       END-IF
       MOVE WS-NAME TO DET-NAME
       WRITE REGION-REPORT-RECORD FROM REGION-DETAIL
       .

   PRINT-HEADER.
       IF HEAD-REGION NOT = SPACE
           MOVE SPACES TO REGION-REPORT-RECORD 
           WRITE REGION-REPORT-RECORD
       END-IF
       MOVE WS-REGION TO HEAD-REGION
                         PRIOR-REGION
       WRITE REGION-REPORT-RECORD FROM REGION-REPORT-HEADER
       .
  • A much nicer example. And I know you know about FILE STATUS. Have you tried the same trick for the `RETURN` to avoid the ugly `AT END/NOT AT END`? You're missing the `END-RETURN`, perhaps the END-PERFORM terminated it? I'll have to have think about that :-) – Bill Woodger Oct 31 '14 at 00:31
0

You should do this in a single pass of the file. Instead of just checking one item at a time, check all of them in the single pass...

RegularExpression
  • 3,531
  • 2
  • 25
  • 36