Menu
CoddyTech

OUTPUT PROCEDURE

Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 17 of 22.

OUTPUT PROCEDURE replaces GIVING: a paragraph takes the sorted records one at a time, to print or total them.

SORT WORK-FILE
    ON DESCENDING KEY W-AMT
    USING SALES-FILE
    OUTPUT PROCEDURE IS PRINT-RANKS.

RETURN gets the next sorted record into the SD record. It works like a READ, with AT END when none are left, and runs in a loop the same way:

RETURN WORK-FILE
    AT END SET SORT-DONE TO TRUE
    NOT AT END DISPLAY W-REP
END-RETURN

One SORT can have both an input and an output procedure: choose the records, sort them, then report on them.

challenge icon

Challenge

Easy

Write PRINT-RANKS: until SORT-DONE, return the next record of WORK-FILE, setting SORT-DONE at the end and performing PRINT-ONE for each record. The ranking starts 1. Raj      500.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. RANKS.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT SALES-FILE ASSIGN TO SALESIN
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT WORK-FILE ASSIGN TO SORTWK.
       DATA DIVISION.
       FILE SECTION.
       FD SALES-FILE.
       01 SALES-REC.
           05 S-REGION PIC X(5).
           05 S-REP PIC X(6).
           05 S-AMT PIC 9(5).
       SD WORK-FILE.
       01 WORK-REC.
           05 W-REGION PIC X(5).
           05 W-REP PIC X(6).
           05 W-AMT PIC 9(5).
       WORKING-STORAGE SECTION.
       01 WS-DONE PIC X VALUE "N".
           88 SORT-DONE VALUE "Y".
       01 WS-RANK PIC 9 VALUE 0.
       01 WS-AMT PIC ZZ,ZZ9.
       PROCEDURE DIVISION.
       MAIN-PARA.
           SORT WORK-FILE
               ON DESCENDING KEY W-AMT
               USING SALES-FILE
               OUTPUT PROCEDURE IS PRINT-RANKS.
           STOP RUN.
       PRINT-RANKS.
           *> Return each sorted record here
       PRINT-ONE.
           ADD 1 TO WS-RANK.
           MOVE W-AMT TO WS-AMT.
           DISPLAY WS-RANK ". " W-REP WS-AMT.

All lessons in Files and Reports