 IDENTIFICATION DIVISION.
 PROGRAM-ID.  Sort2.
 ENVIRONMENT DIVISION.
 INPUT-OUTPUT SECTION.
 FILE-CONTROL.
     SELECT MAILING-INFO         ASSIGN TO INDATA1.
     SELECT MAILING-OUTPUT       ASSIGN TO OUTDATA.
     SELECT SORT-WORK            ASSIGN TO SORTWORK.

 DATA DIVISION.
 FILE SECTION.
 FD  MAILING-INFO.
     LABEL RECORDS STANDARD.
 01  MAILING-INFO-RECORD.
     05  FILLER                    PIC X(27).
     05  MAILING-ADDRESS           PIC X(120).
     05  FILLER                    PIC X(50).

 FD  MAILING-OUTPUT.
 01  MAILING-OUTPUT-RECORD.
     05  MAIL-CONTROL-BREAK-CODE    PIC XXX.
     05  MAIL-BREAK-ZIP-CODE        PIC X(5).
     05  MAIL-BREAK-COUNT           PIC 9(9).
     05  FILLER                     PIC X(111).
 01  MAILING-PRINT-RECORD           PIC X(132).

SD  SORT-WORK.
 01  SORT-RECORD.
     05  SORT-FULL-ZIP-CODE         PIC X(9).
     05  SORT-MAILING-INFO          PIC X(197).

 WORKING-STORAGE SECTION.
 01  END-INPUT-STATUS               PIC XXX VALUE ‘NO ‘.
     88  END-INPUT                          VALUE ‘YES’.
 01  END-SORT-STATUS                PIC XXX VALUE ‘NO ‘.
     88  END-SORT                           VALUE ‘YES’.
 01  PREVIOUS-ZIP-CODE              PIC X(5) VALUE ZERO.
 01  ZIP-CODE-COUNT                 PIC 9(9) VALUE ZERO.

 PROCEDURE DIVISION.
 0000-MAIN-PARA.
     SORT SORT-WORK
         ASCENDING KEY SORT-FULL-ZIP-CODE
         INPUT PROCEDURE 1000-RELEASE-CONTROL
         OUTPUT PROCEDURE 2000-RETURN-CONTROL.
     STOP RUN.

-RELEASE-CONTROL.
     OPEN INPUT MAILING-INFO.
     PERFORM 1200-READ-MAIL-INPUT.
     PERFORM 1100-READ-RELEASE-LOOP
         UNTIL END-INPUT.
     CLOSE MAILING-INFO.

 1100-READ-RELEASE-LOOP.
     CALL ‘FIND-ZIP’ USING MAILING-ADDRESS SORT-FULL-ZIP-CODE.
     MOVE MAILING-INFO-RECORD TO SORT-MAILING-INFO.
     RELEASE SORT-RECORD.
     PERFORM 1200-READ-MAIL-INPUT.

 1200-READ-MAIL-INPUT.
     READ MAILING-INFO
         AT END
           MOVE ‘YES’ TO END-INPUT-STATUS.

 2000-RETURN-CONTROL.
     OPEN OUTPUT MAILING-OUTPUT.
     PERFORM 2200-RETURN-SORT-RECORD.
     MOVE SORT-FULL-ZIP-CODE (1:5) TO PREVIOUS-ZIP-CODE.
     PERFORM 2100-PROCESS-OUTPUT-LOOP
         UNTIL END-SORT.
     MOVE ‘XXX’ TO MAIL-CONTROL-BREAK-code.
     MOVE PREVIOUS-ZIP-CODE TO MAIL-BREAK-ZIP-CODE.
     MOVE ZIP-CODE-COUNT TO MAIL-BREAK-COUNT.
     WRITE MAILING-OUTPUT-RECORD.
     CLOSE MAILING-OUTPUT.

 2100-PROCESS-OUTPUT-LOOP.
     IF SORT-FULL-ZIP-CODE (1:5) NOT = PREVIOUS-ZIP-CODE
         MOVE ‘XXX’ TO MAIL-CONTROL-BREAK-code
         MOVE PREVIOUS-ZIP-CODE TO MAIL-BREAK-ZIP-CODE
         MOVE ZIP-CODE-COUNT TO MAIL-BREAK-COUNT
         WRITE MAILING-OUTPUT-RECORD
         MOVE ZERO TO ZIP-CODE-COUNT
         MOVE SORT-FULL-ZIP-CODE  (1:5) TO PREVIOUS-ZIP-CODE.
     PERFORM 2500-FORMAT-PRINT-OUTPUT.
     ADD 1 TO ZIP-CODE-COUNT.
     PERFORM 2200-RETURN-SORT-RECORD.

 2200-RETURN-SORT-RECORD.
     RETURN SORT-WORK
         AT END
           MOVE ‘YES’ TO END-SORT-STATUS.

 2500-FORMAT-PRINT-OUTPUT.
*    Formatting and printing mailing data will be done here.
     EXIT.
