Use a hash organization and search method for maximum efficiency when performing random keyed access on a table. A summary table will be used here to accumulate the total value for an unknown number of parts. At the end of the job, the totals are to be printed in key sequence. Once a table row is found, the table need not be searched again; it can be referenced directly with any of the COUNT commands, such as Replace by Count.
At end of file for the input data, the table is sorted by changing the definition to sequential and the part number and total for each part are printed.
Note:
If multiple users could be updating a table—whether online, in a Read/Write VTS-TSR or other multitasking environment—be aware that a LOCK-LATCH password is required to ensure exclusive access to a table.
In COBOL
DATA DIVISION.
01 DATA-ROW.
05
05 PARTNO PIC X(8).
05
05 PART-VALUE PIC S9(5) COMP-3.
05
01 SUM-ROW-AREA.
05 SUM-ROW-PARTNO-KEY PIC X(8).
05 SUM-ROW-ACCUM-VALUE PIC S9(11) COMP-3.
01 SUM-COMMAND-AREA.
05 SUM-COMMAND PIC XX VALUE SPACES.
05 SUM-TABLE PIC X(8) VALUE 'TABLE1'.
05 SUM-FOUND PIC X VALUE SPACES.
05 SUM-INDIRECT-OPEN PIC X VALUE LOW VALUES.
05 SUM-RESERVED PIC X VALUE LOW-VALUES.
05 SUM-ABEND-OVERRIDE PIC X VALUE SPACES.
05 SUM-ERROR-CODE PIC S9(4) COMP VALUE +0.
05 SUM-COUNT PIC S9(9) COMP VALUE +0.
05 SUM-LOCK-LATCH PIC X(8) VALUE SPACES.
05 SUM-LENGTHS.
10 SUM-ROW-OVERRIDE-LENGTH PIC S9(9) COMP VALUE +0.
10 SUM-ROW-ACTUAL-LENGTH PIC S9(9) COMP VALUE +0.
10 SUM-FG-KEY-LENGTH PIC S9(4) COMP VALUE +0.
05 SUM-FUNCTION-ID PIC S9(4) COMP VALUE +0.
05 SUM-FUNCTION-AREA PIC X(28) VALUE LOW-VALUES.
05 SUM-DATE-AREA REDEFINES SUM-FUNCTION-AREA.
10 SUM-DATE PIC X(8).
10 RESERVED PIC X(20).
05 SUM-RETURNED-ABS-GEN-NO PIC S9(4) COMP VALUE +0.
05 SUM-ERROR-SUBCODE PIC S9(4) COMP VALUE +0.
01 SUM-DEFINITION-BLOCK.
05 SUM-ORG PIC X.
05 SUM-METHOD PIC X.
05 SUM-INDEX PIC X VALUE 'P'.
05 SUM-SMC PIC X VALUE SPACES.
05 SUM-RPSWD PIC X(8) VALUE SPACES.
05 SUM-WPSWD PIC X(8) VALUE SPACES.
05 SUM-RSZ PIC S9(9) COMP VALUE +14.
05 SUM-KSZ PIC S9(9) COMP VALUE +8.
05 SUM-KLOC PIC S9(9) COMP VALUE +1.
05 SUM-ROWS PIC S9(9) COMP VALUE +500.
05 SUM-GENERATIONS PIC S9(4) COMP VALUE +1.
05 SUM-EXP-FACT PIC S9(4) COMP VALUE +200.
05 SUM-LO-DEN PIC S9(4) COMP VALUE +500.
05 SUM-HI-DEN PIC S9(4) COMP VALUE +800.
05 FILLER PIC X(6).
05 SUM-DATE-TIME PIC X(12) VALUE SPACES.
05 SUM-ABS-GEN-NO PIC S9(4) COMP VALUE +0.
05 SUM-DATASET-NAME PIC X(44).
05 SUM-REL-GEN-NO PIC S9(4) COMP.
05 SUM-GENS-PRESENT PIC S9(4) COMP.
05 SUM-ROWS-AT-EXPAND PIC S9(9) COMP.
05 SUM-DDNAME PIC X(8).
05 SUM-DATA-TABLE PIC X(8).
05 SUM-OPEN-STATUS PIC X.
05 SUM-ALTS-INVOKED PIC X.
05 SUM-VIEW-VERSION PIC X.
05 FILLER PIC X.
05 SUM-USERID PIC X(8).
05 SUM-VIEW-NAME PIC X(8).
05 SUM-VIEW-DATE PIC X(12).
05 SUM-USER-COMMENTS PIC X(16).
05 SUM-VTSNAME PIC X(8) VALUE SPACES.
05 FILLER PIC X(68).
PROCEDURE DIVISION.
PROCESS-ROUTINE.
PERFORM DEFINE-TABLE
PERFORM POPULATE-TABLE
PERFORM CHANGE-TO-SEQUENTIAL-ORDER
PERFORM PRINT-TABLE
PERFORM CLOSE-TABLE
GOBACK.
DEFINE-TABLE
MOVE 'H' TO SUM-ORG
MOVE 'H' TO SUM-METHOD
MOVE 'DT' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-DEFINITION-BLOCK
EXIT.
POPULATE-TABLE.
PERFORM OBTAIN-INPUT-DATA-RECORD
PERFORM UNTIL (no more data records to be processed)
MOVE PARTNO TO SUM-ROW-PARTNO-KEY
*** SEARCH THE TABLE FOR MATCHING PARTNO.
MOVE 'FK' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-ROW-AREA
SUM-ROW-PARTNO-KEY
IF SUM-FOUND = 'Y'
*** MATCHING PARTNO FOUND. UPDATE AND REPLACE TABLE ROW
ADD PART-VALUE TO SUM-ROW-ACCUM-VALUE
MOVE 'RC' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-ROW-AREA
ELSE
*** MATCHING PARTNO NOT FOUND. INSERT NEW TABLE ROW
MOVE PART-VALUE TO SUM-ROW-ACCUM-VALUE
MOVE 'IC' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-ROW-AREA
END-IF
PERFORM OBTAIN-INPUT-DATA-RECORD
END-PERFORM
EXIT.
*** RE-SEQUENCE THE TABLE FOR PRINTING.
CHANGE-TO-SEQUENTIAL-ORDER.
MOVE 'S' TO SUM-ORG
MOVE 'B' TO SUM-METHOD
MOVE 'CD' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-DEFINITION-BLOCK
EXIT.
*** PRINT ALL ROWS IN THE TABLE.
PRINT-TABLE.
MOVE 'GF' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-ROW-AREA
MOVE 'GN' TO SUM-COMMAND
PERFORM UNTIL SUM-FOUND = 'N'
DISPLAY 'PART NUMBER ' SUM-ROW-PARTNO-KEY
' QUANTITY - ' SUM-ROW-ACCUM-VALUE
CALL 'TBLBASE' USING TB-PARM
SUM-COMMAND-AREA
SUM-ROW-AREA
END-PERFORM
EXIT.
CLOSE-TABLE.
MOVE 'CL' TO SUM-COMMAND
CALL 'TBLBASE' USING TB-PARM