[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL program Direct Subscripting

Direct Subscripting in COBOL


12. Direct Subscripting

DIRSUB1.

This is direct subscripting - you know the subscript, you use it to reference the desired occurrence in the table. Given an 8 on the input record, you want AUGUST.

It would be a good idea to look up explanations of subscripts in your COBOL book.

Direct subscripting probably won’t be the only thing you do in a program. It will most likely be used in a larger, more complex program, such as a report program. It will allow you to change fields on the input record, based on a table of items that are always numbered in the same way. A good example would be the months of the year: January is always 1 and December is always 12.

Here is the program DIRSUB1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.



000200 IDENTIFICATION DIVISION.
000300* DIRECT SUBSCRIPTING
000400 PROGRAM-ID. DIRSUB1.
000500*  GENERAL LOGIC FOR PROGRAM THAT READS EVERY INPUT RECORD
000600*  AFTER LOOKING UP THE EMPLOYEE'S MONTH OF HIRE ON A TABLE,
000700*      BY DIRECT SUBSCRIPTING,
000800*  IT WRITES IT OUT TO AN OUTPUT FILE
000900 ENVIRONMENT DIVISION.
001000 INPUT-OUTPUT SECTION.
001100 FILE-CONTROL.
001200*    INPUT FILE EMP
001300     SELECT INPUT-FILE ASSIGN EMP.
001700*    REPORTFI: A REPORT FILE, PRINTS OUT INFORMATION ON EMPLOYEES
001800*        WITH MONTH OF HIRE, SEND TO PRINTER
001900     SELECT REPORT-FILE ASSIGN REPORTFI.
002300
002400 DATA DIVISION.
002500 FILE SECTION.
002600
002700 FD  INPUT-FILE
002800     RECORDING MODE IS F
003100     RECORD CONTAINS 80 CHARACTERS.
003200 01  INPUT-RECORD.
003210*            INPUT RECORD DESCRIPTION
003230     05  FILLER                        PIC X(8).
003230     05  FILLER                        PIC X(01).
003240     05  ER-EMPLOYEE-NUMBER            PIC X(05).
003250     05  FILLER                        PIC X(01).
003260     05  ER-EMPLOYEE-NAME              PIC X(25).
003270*    05  FILLER                        PIC X(01).
003280     05  ER-EMPLOYEE-DEPARTMENT        PIC X(05).
003290     05  FILLER                        PIC X(01).
003300     05  ER-EMPLOYEE-SALARY-CODE       PIC X(02).
003310     05  FILLER                        PIC X(01).
003320*     MONTH OF HIRE MUST BE DEFINED AS NUMERIC,
003330*      TO USE DIRECT SUBSCRIPTING
003340     05  ER-MONTH-OF-HIRE              PIC 9(02).
003350     05  FILLER                        PIC X(28).
003360
003400 FD  REPORT-FILE
003500     RECORDING MODE IS F
003800     RECORD CONTAINS 133 CHARACTERS.
003900
004000 01  REPORT-RECORD                      PIC X(133).
004100
004200 WORKING-STORAGE SECTION.
004300
004400 01  FILE-AT-END             PIC X VALUE 'N'.
004500*
004600 01  SW-VALID-RECORD         PIC X VALUE 'Y'.
004700
006700 01  COUNTERS-AND-ACCUMULATORS.
006800     05  CTR-RECORDS-READ               PIC  S9(5)
006900             PACKED-DECIMAL  VALUE +0.
007000     05  CTR-RECORDS-WRITTEN            PIC S9(5)
007100             PACKED-DECIMAL  VALUE +0.
007200
007300 01  TITLE-HEADING-LINE.
007400     05  FILLER                         PIC X(1) VALUE SPACES.
007500     05  FILLER                         PIC X(35)
007600                   VALUE 'EMPLOYEE RECORDS WITH MONTH OF HIRE'.
007700     05  FILLER                         PIC X(04) VALUE SPACES.
007800     05  FILLER                         PIC X(33)
007900                   VALUE SPACES.
008000     05  REPORT-DATE                    PIC X(8).
008100
008200 01  DETAIL-PRINT-LINE.
008300     05  FILLER                         PIC X(1) VALUE SPACES.
008400     05  DL-MONTH-OF-HIRE               PIC X(09).
008500     05  DL-RECORD-IMAGE                PIC X(80) VALUE SPACES.
008600
008700 01  MONTH-TABLE-LITERALS.
008800       05 FILLER PIC X(9) VALUE 'JANUARY'.
008900       05 FILLER PIC X(9) VALUE 'FEBRUARY'.
009000       05 FILLER PIC X(9) VALUE 'MARCH'.
009100       05 FILLER PIC X(9) VALUE 'APRIL'.
009200       05 FILLER PIC X(9) VALUE 'MAY'.
009300       05 FILLER PIC X(9) VALUE 'JUNE'.
009400       05 FILLER PIC X(9) VALUE 'JULY'.
009500       05 FILLER PIC X(9) VALUE 'AUGUST'.
009600       05 FILLER PIC X(9) VALUE 'SEPTEMBER'.
009700       05 FILLER PIC X(9) VALUE 'OCTOBER'.
009800       05 FILLER PIC X(9) VALUE 'NOVEMBER'.
009900       05 FILLER PIC X(9) VALUE 'DECEMBER'.
010000
010100 01  MONTH-TABLE REDEFINES MONTH-TABLE-LITERALS.
010200       05 EACH-MONTH PIC X(9) OCCURS 12 TIMES.
010300
010400 PROCEDURE DIVISION.
010500     PERFORM INITIALIZATION
010600     PERFORM PROCESS-ALL UNTIL
010700        FILE-AT-END = 'Y'
010800     PERFORM TERMINATION
010900     GOBACK.
011000
011100 INITIALIZATION.
011200     OPEN INPUT INPUT-FILE
011300          OUTPUT REPORT-FILE
011400     WRITE REPORT-RECORD FROM TITLE-HEADING-LINE
011500     PERFORM READ-PAR
011600
011700     ACCEPT REPORT-DATE FROM DATE.
011800
011900 PROCESS-ALL.
012000     MOVE EACH-MONTH(ER-MONTH-OF-HIRE) TO DL-MONTH-OF-HIRE
012100     MOVE INPUT-RECORD TO DL-RECORD-IMAGE
012200     WRITE REPORT-RECORD FROM DETAIL-PRINT-LINE
012300
012400     PERFORM READ-PAR.
012500
012600 TERMINATION.
012700
012800     CLOSE INPUT-FILE
012900           REPORT-FILE.
013000
013100 READ-PAR.
013200     READ INPUT-FILE 
013300     AT END
013400         MOVE 'Y' TO FILE-AT-END
013500     NOT AT END
013600        ADD 1 TO CTR-RECORDS-READ
013700     END-READ.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file EMP: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 01000 PEARLE E GATES D0001 01 02000 LED BALOON D0002 04 03000 KIT E LITTER D0005 06 04000 MOE PEDD D0504 01 Here is sample JCL: //STEP1 EXEC PGM=DIRSUB1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //EMP DD DSN=userid.COBBOOK.DATA(EMP),DISP=SHR //REPORTFI DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |