Professional Documents
Culture Documents
Ch10 Slides
Ch10 Slides
Objectives
Applied
y Given specifications for a program that uses a one-level or a multilevel table, develop the program using either subscripts or indexes. This may require loading a table, searching a table, or processing the entries in a table.
Knowledge
y Explain why using indexes is more efficient than using subscripts. y List three ways that you can refer to a table entry using subscripts and three ways that you can refer to a table entry using indexes. y Describe the difference between a sequential and a binary search. y In general terms, describe the difference between working with a fixed-length table and a variable-length table. y Explain how you can use the entries in a table with intrinsic functions.
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 2
The COBOL description for a one-level table that contains constant values
01 MONTH-TABLE-VALUES. 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC X(9) X(9) X(9) X(9) X(9) X(9) X(9) X(9) X(9) X(9) X(9) X(9) VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE "JANUARY ". "FEBRUARY ". "MARCH ". "APRIL ". "MAY ". "JUNE ". "JULY ". "AUGUST ". "SEPTEMBER". "OCTOBER ". "NOVE MBER ". "DECEMBER ".
01
How to initialize a ta le
In workin storage
01 PRICE-TABLE. 05 PRICE-GROUP 10 ITEM-NUMBER 10 ITEM-PRICE OCCURS 16 TIMES. PIC 9(3) VALUE ZERO. PIC S99V99 VALUE ZERO.
Note
y You can also code the Value clause on a group ield to initialize all the ields in that group to the same value.
Code or defi i
01
le wit
se
onst nt
l es
Using t e Redefines l
PRICE-TABLE-VALUES. 05 FILLER 05 FILLER . . 05 FILLER 05 FILLER
01
PRICE-TABLE REDEFINES PRICE-TABLE-VALUES. 05 PRICE-GROUP OCCURS 16 TIMES. 10 ITEM-NUMBER PIC 9(3). 10 ITEM-PRICE PIC S99V99.
Using t e V l e l
01
se
PRICE-TABLE VALUE "1011250107500011107701580555". 05 PRICE-GROUP OCCURS 4 TIMES. 10 ITEM-NUMBER PIC 9(3). 10 ITEM-PRICE PIC S99V99.
How o ub p
o n
on
u ng
y You can refer to table entries using subscripts, occurrence numbers, or relative subscripting. y A subscript is a field that contains an occurrence number, which indicates the occurrence of the field you want to refer to. y A subscript must be defined as an integer, and it should be defined with binary usage for efficiency. y You can use relative subscripts to increase or decrease a subscript value by a literal value when you refer to a table entry. y You can use these techniques to refer to a group field or an individual field. y You can use the same subscript to refer to entries in two or more tables, and you can use two or more subscripts to refer to the entries in a single table.
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 9
PIC X
BINARY. PIC S99. VALUE ZERO. OCCURS 16 TIMES. PIC 9(3). PIC S99V99.
01
01
BINARY. PIC S99. VALUE ZERO. OCCURS 16 TIMES. PIC 9(3). PIC S99V99.
01
01
PIC S99V99.
01
A two-level table
Age range lass 10 lass 20 lass 30 lass 40
2 .0 2 . 2 .1 2 . 2 . 1.
.2 . . .0 .0 .
40 44 4 4 0 4
At
Age 1 4 5 40 44 45 4 50 54 55 5
-l
lt
l
M Cl 2 2 .05 2 .55 2 .15 2 . 5 2 . 5 1.55 Cl 24. 5 25. 0 2 .10 2 .10 1.55 5.00 W 1 Cl 2 2 .45 2 .50 0. 0 2. 0 5.25 . 0
e COBOL escri ti
01
f rt et
le
OCCURS 6 TIMES. PIC 99. PIC 99. OCCURS 2 TIMES. OCCURS 2 TIMES PIC S99V99.
A reference t t e t
le
UR clause ithin
y To re er to an entry in a multi-level table, you code a subscript name, occurrence number, or relative subscript or each level separated by spaces and enclosed in parentheses.
RATE-TABLE. 05 AGE-GROUP 10 HIGH-AGE 10 CLASS-GROUP 15 CLASS-NUMBER 15 INSURANCE-RATE RATE-TABLE-RECORD. 05 RT-HIGH-AGE 05 RT-CLASS-GROUP 10 RT-CLASS-NUMBER 10 RT-INSURANCE-RATE . .
OCCURS 6 TIMES. PIC 99. OCCURS 4 TIMES. PIC 99. PIC S99V99.
01
PIC S99V99.
01
OCCURS 6 TIMES. PIC 99. OCCURS 4 TIMES. PIC 99. PIC S99V99.
01
Cod b
o ip
hi ( o i
ol d)
bl
PROCEDURE DIVISION. . . 350-SEARCH-RATE-TABLE. MOVE "N" TO RATE-FOUND-S ITCH. PERFORM 360-SEARCH-AGE-GROUP ITH TEST AFTER VARYING AGE-SUB FROM 1 BY 1 UNTIL RATE-FOUND OR AGE-SUB = 6. IF RATE-FOUND MOVE INSURANCE-RATE (AGE-SUB CLASS-SUB) TO POLICY-RATE. . .
ith an index
Cod fo lo di
-l
l t bl
i d
PIC X
VALUE ZERO. OCCURS 16 TIMES INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.
? END - SEARCH A
y You may ant to use the Varying clause i you associate more than one index ith a table. I you omit this clause, the irst index named on the Indexed y clause or the table is used.
? AND
equal -
nditi n - 2 A ...
10 10
ITEM-NUMBER ITEM-PRICE
. . SEARCH ALL PRICE-GROUP AT END MOVE "N" TO ITEM-FOUND-SWITCH WHEN ITEM-NUMBER (PRICE-TABLE-INDEX) = TR-ITEM-NO SET ITEM-FOUND TO TRUE.
Cod for i d
r hi
two-l
l t bl
PIC S99V99.
OCCURS 6 TIMES INDEXED BY AGE-INDEX. PIC 99. OCCURS 4 TIMES INDEXED BY CLASS -INDEX. PIC 99. PIC S99V99.
10 10
ITEM-NUMBER ITEM-PRICE
T e definition of t e Depending
05 PT-ENTRY-COUNT PIC S9(3).
n field
PIC S9(3).
10 10 01
ITEM-NUMBER ITEM-PRICE
OCCURS 1 TO 100 TIMES DEPENDING ON PT-ENTRY-COUNT ASCENDING KEY IS ITEM-NUMBER INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.
INDEX.
OCCURS 100 TIMES INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.
? procedure - name A
identifier - 2 identifier - 1 VARYING FROM index - name - 2 index - name - 1 integer - 1 BY identifier - 3 UNTIL integer - 2
identifier - 5 AFTER _index - name - 3 a FROM index - name - 4 integer - 3 ... identifier - 6 BY UNTIL condition - 2 integer - 4 ? statement - 1 A ...
? END - PERFORM A
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 51
condition - 1
Cod t
t o-l
l P rform t t m nt
PIC S9(3)V99.
CLASS-NUMBER INSURANCE-RATE
OCCURS 6 TIMES INDEXED BY A GE-INDEX. PIC 99. OCCURS 4 TIMES INDEXED BY CLASS -INDEX. PIC 99. PIC S99V99.
. PROCEDURE DIVISION. . PERFORM WITH TEST AFTER VARYING AGE-INDEX FROM 1 BY 1 UNTIL AGE-INDEX = 6 AFTER CLASS-INDEX FROM 1 BY 1 UNTIL CLASS-INDEX = 4 ADD INSURANCE-RATE (AGE-INDEX CLASS-INDEX) TO RATE-T0TAL.
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 52
Arguments
Numeric series Numeric series Numeric series Numeric series Numeric series Numeric series Alphanumeric series Alphanumeric series Alphanumeric or numeric series Alphanumeric or numeric series Numeric series (1)Interest rate as a decimal fraction; (2) Numeric series of future pa ments
Result
he mean of the ar uments. he median of the ar uments. he standard de iation of the ar uments. he ariance of the ar uments. he alue of the ma imum ar ument minus the alue of the minimum ar ument. he mean of the ma imum and minimum ar uments. he alue of the lar est ar ument. he alue of the smallest ar ument. he ordinal position in the ar ument list of the lar est ar ument. he ordinal position in the ar ument list of the smallest ar ument. he sum of the ar uments. he present alue of a series of future pa ments (ar ument 2) discounted at the interest rate of ar ument 1.
Chapter 10, Slide 54
A function that calculates the mean of all the insurance rates in the first age group
FUNCTION MEAN (INSURANCE -RATE (1 ALL))