>>SOURCE FORMAT IS FREE *> cobol-occurs-subscripts-table.cob — monthly totals in an OCCURS table *> Dialect: GnuCOBOL 3.x, free format *> Status : desk-checked against the GnuCOBOL 3.2 Programmer's Guide; *> NOT compiled (no cobc on the authoring machine) *> Inputs : none (the twelve monthly figures are VALUE clauses) *> Outputs: DISPLAY lines on the console *> Build : cobc -x cobol-occurs-subscripts-table.cob *> Run : ./cobol-occurs-subscripts-table *> (Windows: cobol-occurs-subscripts-table.exe) IDENTIFICATION DIVISION. PROGRAM-ID. MONTHLY-TOTALS. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. *> Twelve figures given compile-time values, then viewed as a table *> through REDEFINES (the technique the Programmer's Guide documents *> under VALUE). 01 Month-Values. 05 FILLER PIC 9(7)V99 VALUE 125430.50. 05 FILLER PIC 9(7)V99 VALUE 98210.00. 05 FILLER PIC 9(7)V99 VALUE 143875.25. 05 FILLER PIC 9(7)V99 VALUE 110004.10. 05 FILLER PIC 9(7)V99 VALUE 156320.00. 05 FILLER PIC 9(7)V99 VALUE 171009.99. 05 FILLER PIC 9(7)V99 VALUE 132500.00. 05 FILLER PIC 9(7)V99 VALUE 99875.40. 05 FILLER PIC 9(7)V99 VALUE 148200.75. 05 FILLER PIC 9(7)V99 VALUE 160010.00. 05 FILLER PIC 9(7)V99 VALUE 189430.20. 05 FILLER PIC 9(7)V99 VALUE 210655.55. 01 Month-Table REDEFINES Month-Values. 05 Month-Total OCCURS 12 TIMES PIC 9(7)V99. 01 Month-Names-Values PIC X(36) VALUE "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC". 01 Month-Names REDEFINES Month-Names-Values. 05 Month-Name OCCURS 12 TIMES PIC X(3). *> A table filled at run time. 01 Quarter-Table. 05 Quarter-Total OCCURS 4 TIMES PIC 9(8)V99. *> A two-dimensional table: OCCURS inside OCCURS. 01 Grid. 05 Grid-Row OCCURS 3 TIMES. 10 Grid-Cell OCCURS 4 TIMES PIC 9(3). *> A variable-length table: room for 12, but only Active-Months count. 77 Active-Months PIC 99 VALUE 9. 01 YTD-Table. 05 YTD-Total OCCURS 1 TO 12 TIMES DEPENDING ON Active-Months PIC 9(7)V99. 77 M PIC 99. 77 Q PIC 9. 77 R PIC 9. 77 C PIC 9. 77 Max-Month PIC 99 VALUE 1. 77 Grand-Total PIC 9(9)V99 VALUE 0. 77 Show-Amt PIC Z(6)9.99. 77 Show-Total PIC Z(8)9.99. PROCEDURE DIVISION. Main-Paragraph. *> Walk the table once: print, sum, and remember the largest entry. PERFORM VARYING M FROM 1 BY 1 UNTIL M > 12 MOVE Month-Total(M) TO Show-Amt DISPLAY Month-Name(M) " " Show-Amt ADD Month-Total(M) TO Grand-Total IF Month-Total(M) > Month-Total(Max-Month) MOVE M TO Max-Month END-IF END-PERFORM MOVE Grand-Total TO Show-Total DISPLAY "Year total " Show-Total DISPLAY "Best month " Month-Name(Max-Month) *> A subscript is a literal, an identifier, or an arithmetic expression. DISPLAY "First month " Month-Name(1) MOVE 6 TO M DISPLAY "Month M + 1 " Month-Name(M + 1) PERFORM VARYING Q FROM 1 BY 1 UNTIL Q > 4 COMPUTE Quarter-Total(Q) = Month-Total(3 * Q - 2) + Month-Total(3 * Q - 1) + Month-Total(3 * Q) MOVE Quarter-Total(Q) TO Show-Total DISPLAY "Quarter " Q " " Show-Total END-PERFORM *> Two subscripts for two dimensions; AFTER drives the inner loop. PERFORM VARYING R FROM 1 BY 1 UNTIL R > 3 AFTER C FROM 1 BY 1 UNTIL C > 4 COMPUTE Grid-Cell(R, C) = R * 10 + C END-PERFORM PERFORM VARYING R FROM 1 BY 1 UNTIL R > 3 DISPLAY "Row " R ": " Grid-Cell(R, 1) " " Grid-Cell(R, 2) " " Grid-Cell(R, 3) " " Grid-Cell(R, 4) END-PERFORM *> OCCURS DEPENDING ON: the storage is always the maximum, but the *> table logically has Active-Months entries. MOVE 0 TO Grand-Total PERFORM VARYING M FROM 1 BY 1 UNTIL M > Active-Months MOVE Month-Total(M) TO YTD-Total(M) ADD YTD-Total(M) TO Grand-Total END-PERFORM MOVE Grand-Total TO Show-Total DISPLAY "YTD through " Month-Name(Active-Months) " " Show-Total STOP RUN.