Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 55

Chapter 10

How to work with tables

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 1

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 basic syntax of a table definition


level-number data-name OCCURS integer TIMES

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

MONTH-TABLE REDEFINES MONTH-TABLE-VALUES. 05 MONTH-NAME PIC X(9) OCCURS 12 TIMES.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 3

A one-level table that will be loaded f om a file


The data for the table
Item number 101 107 111 158 161 192 201 213 Price 12.50 50.00 7.70 5.55 62.50 25.00 .40 6.66 Item number 277 297 305 341 342 343 347 351 Price 1.11 7.77 .10 15.00 57.50 65.00 22.50 .35

The COBOL description for the table


01 PRICE-TABLE. 05 PRICE-GROUP OCCURS 16 TIMES. 10 ITEM-NUMBER PIC 9(3). 10 ITEM-PRICE PIC S99V99.
2004, Mike Murach & Associates, Inc. Chapter 10, Slide 4

Murachs Mainframe COBOL

How to efine a one-le el ta le t at uses su scri ts


y A one-level table contains data that depends on a single variable actor. y The ccurs clause indicates ho many times a ield or a group o ields is repeated in storage and can be coded rom level 02 through 49. Each occurrence holds one table entry. y A table can contain constant values, hich can be coded or copied directly into the program, or variable values, hich can be loaded rom a ile.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 5

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.

From t e Proce ure Di ision


INITIALIZE PRICE-TABLE.

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.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 6

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

PIC 9(3) PIC S99V99

VALUE "101". VALUE +12.50.

PIC 9(3) PIC S99V99

VALUE "351". VALUE +.35.

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.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 7

The syntax for referring to a table entry in a onelevel table


subscript - name ?_ data - name occurrence - number
- a literal A

Statements that refer to entries in a one-level table


MOVE MONTH-NAME (6) TO HDG3-MONTH-NAME. COMPUTE LINE-ITEM-EXTENSION ROUNDED = TR-QUANTITY * ITEM-PRICE (PRICE-TABLE-SUB). MOVE MONTH-NAME (MONTH-SUB - 1) TO HDG3-PRIOR-MONTH-NAME.

The definition of a subscript


05 PRICE-TABLE-SUB PIC S99 BINARY.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 8

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

Code for loading a one-level ta le using su scri ts


WORKING-STORAGE SECTION. . . 01 SWITCHES. 05 PTABLE-EOF-SWITCH 88 PTABLE-EOF 01 SUBSCRIPTS 05 PRICE-TABLE-SUB PRICE-TABLE 05 PRICE-GROUP 10 ITEM-NUMBER 10 ITEM-PRICE PRICE-TABLE-RECORD. 05 PT-ITEM-NUMBER 05 PT-ITEM-PRICE . .

PIC X

VALUE "N". VALUE "Y".

BINARY. PIC S99. VALUE ZERO. OCCURS 16 TIMES. PIC 9(3). PIC S99V99.

01

01

PIC 9(3). PIC S99V99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 10

Code for loading a one-level table using subscripts (continued)


PROCEDURE DIVISION. . 100-LOAD-PRICE-TABLE. PERFORM WITH TEST AFTER VARYING PRICE-TABLE-SUB FROM 1 BY 1 UNTIL PTABLE-EOF OR PRICE-TABLE-SUB = 16 PERFORM 110-READ-PRICE-TABLE-RECORD IF NOT PTABLE-EOF MOVE PT-ITEM-NUMBER TO ITEM-NUMBER (PRICE-TABLE-SUB) MOVE PT-ITEM-PRICE TO ITEM-PRICE (PRICE-TABLE-SUB). 110-READ-PRICE-TABLE-RECORD. READ PTABLE RECORD INTO PRICE-TABLE-RECORD AT END SET PTABLE-EOF TO TRUE. . .
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 11

Code for searching a one-level table using subscripts


WORKING-STORAGE SECTION. . 01 SWITCHES. 05 ITEM-FOUND-SWITCH 88 ITEM-FOUND 01 SUBSCRIPTS 05 PRICE-TABLE-SUB PRICE-TABLE 05 PRICE-GROUP 10 ITEM-NUMBER 10 ITEM-PRICE WORK-FIELDS. 05 UNIT-PRICE TRANSACTION-RECORD. 05 TR-REFERENCE-CODE 05 TR-REFERENCE-DATE 05 TR-CUSTOMER-NUMBER 05 TR-ITEM-NUMBER PIC X VALUE "N". VALUE "Y".

BINARY. PIC S99. VALUE ZERO. OCCURS 16 TIMES. PIC 9(3). PIC S99V99.

01

01

PIC S99V99.

01

PIC PIC PIC PIC

X(6). X(8). 9(5). 9(3).

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 12

Code for searching a one-level table using subscripts (continued)


. . PROCEDURE DIVISION. . . MOVE "N" TO ITEM-FOUND-SWITCH. PERFORM 350-SEARCH-PRICE-TABLE WITH TEST AFTER VARYING PRICE-TABLE-SUB FROM 1 BY 1 UNTIL ITEM-FOUND OR PRICE-TABLE-SUB = 16. IF ITEM-FOUND MOVE ITEM-PRICE (PRICE-TABLE-SUB) TO UNIT-PRICE. . . 350-SEARCH-PRICE-TABLE. IF ITEM-NUMBER (PRICE-TABLE-SUB) = TR-ITEM-NUMBER SET ITEM-FOUND TO TRUE. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 13

A two-level table
Age range lass 10 lass 20 lass 30 lass 40

2 . 0 24.00 24. 0 2 . 0 2 . 0 2 .00

2 .0 2 . 2 .1 2 . 2 . 1.

.2 . . .0 .0 .

2. 0 .40 4.00 4. 0 . 0 .40

40 44 4 4 0 4

The OBOL description for the table


01 RATE-TABLE. 05 AGE-GROUP 10 HIGH-AGE 10 CLASS-GROUP 15 CLASS-NUMBER 15 INSURANCE-RATE OCCURS 6 TIMES. PIC 99. OCCURS 4 TIMES. PIC 99. PIC S99V99.

A reference to the table


MOVE INSURANCE-RATE (AGE-SUB CLASS-SUB) TO POLICY-RATE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 14

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

ge Cl 1 2 .50 24.00 24. 0 25. 0 2 . 0 2 .00

e COBOL escri ti
01

f rt et

le

RATE-TABLE. 05 AGE-GROUP 10 LOW-AGE 10 HIGH-AGE 10 SEX-GROUP 15 INSURANCE-RATE

OCCURS 6 TIMES. PIC 99. PIC 99. OCCURS 2 TIMES. OCCURS 2 TIMES PIC S99V99.

A reference t t e t

le

MOVE INSURANCE-RATE (AGE-SUB SEX-SUB CLASS-SUB) TO POLICY-RATE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 15

How to work wit multi-level ta les


y A multi-level table contains data that depends on t o or more actors. y To de ine a multi-level table, you code an an UR clause.

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.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 16

Code for loading a t o-level ta le using su scri ts


WORKING-STORAGE SECTION. . . 01 SUBSCRIPTS 05 AGE-SUB 05 CLASS-SUB 01 BINARY. PIC S9. PIC S9.

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 99. OCCURS 4 TIMES. PIC 99. PIC S99V99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 17

Code for loading a two-level table using subscripts continued


PROCEDURE DIVISION. . . 100-LOAD-RATE-TABLE. PERFORM 110-LOAD-RATE-TABLE-ENTRY WITH TEST AFTER VARYING AGE-SUB FROM 1 BY 1 UNTIL AGE-SUB = 6 OR RATE-TABLE-EOF.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 18

Code for lo din two level t ble usin subscripts (continued)


110-LOAD-RATE-TABLE-ENTRY. PERFORM 120-READ-RATE-TABLE-RECORD. IF NOT RATE-TABLE-EOF MOVE RT-HIGH-AGE TO HIGH-AGE (AGE-SUB) PERFORM WITH TEST AFTER VARYING CLASS-SUB FROM 1 BY 1 UNTIL CLASS-SUB = 4 MOVE RT-CLASS-NUMBER (CLASS-SUB) TO CLASS-NUMBER (AGE-SUB CLASS-SUB) MOVE RT-INSURANCE-RATE (CLASS-SUB) TO INSURANCE-RATE (AGE-SUB CLASS-SUB). 120-READ-RATE-TABLE-RECORD. READ RTABLE RECORD INTO RATE-TABLE-RECORD AT END SET RATE-TABLE-EOF TO TRUE. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 19

Code for earc ing a two-level ta le u ing u cri t


WORKING-STORAGE SECTION. . . 01 SUBSCRIPTS 05 AGE-SUB 05 CLASS-SUB 01 WORK-FIELDS. 05 POLICY-RATE BINARY. PIC S9. PIC S9.

PIC S99V99.

01

RATE-TABLE. 05 AGE-GROUP 10 HIGH-AGE 10 CLASS-GROUP 15 CLASS-NUMBER 15 INSURANCE-RATE APPLICANT-RECORD. 05 AR-AGE 05 AR-CLASS . .

OCCURS 6 TIMES. PIC 99. OCCURS 4 TIMES. PIC 99. PIC S99V99.

01

PIC 99. PIC 99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 20

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. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 21

Code for searching a two-level table using subscripts (continued)


360-SEARCH-AGE-GROUP. IF AR-AGE <= HIGH-AGE (AGE-SUB) PERFORM 370-SEARCH-CLASS-GROUP WITH TEST AFTER VARYING CLASS-SUB FROM 1 BY 1 UNTIL RATE-FOUND OR CLASS-SUB = 4. 370-SEARCH-CLASS-GROUP. IF CLASS-NUMBER (AGE-SUB CLASS-SUB) = AR-CLASS SET RATE-FOUND TO TRUE. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 22

The syntax of a ta le definition

ith an index

level-number data-name OCCURS integer TIMES INDEXED BY {index-name-1} ...

A one-level ta le that uses an index


Ta le definition
01 PRICE-TABLE. 05 PRICE-GROUP 10 10 ITEM-NUMBER ITEM-PRICE OCCURS 16 TIMES INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.

Statements that refer to entries in the ta le


COMPUTE LINE-ITEM-EXTENSION ROUNDED = TR-QUANTITY * ITEM-PRICE (PRICE-TABLE-INDEX). MOVE ITEM-PRICE (1) TO UNIT-PRICE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 23

A two-level table that uses indexes


Table definition
01 RATE-TABLE. 05 AGE-GROUP 10 10 HIGH-AGE CLASS-GROUP 15 15 CLASS-NUMBER INSURANCE-RATE OCCURS 6 TIMES INDEXED BY AGE-INDEX. PIC 99. OCCURS 4 TIMES INDEXED BY CLASS-INDEX. PIC 99. PIC S99V99.

Statements that refer to entries in the table


MOVE INSURANCE-RATE (AGE-INDEX CLASS-INDEX) TO POLICY-RATE. MOVE INSURANCE-RATE (AGE-INDEX CLASS-INDEX + 2) TO POLICY-RATE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 24

Basic skills for working with indexes


y An index represents a displacement value from the start of the table. y Indexes are more efficient to use than subscripts, which have to be converted to displacement values. y You define a table that uses an index the same way you define a table that uses a subscript except that you include the Indexed By clause. y The Indexed By clause names one or more indexes that will be used to refer to the table entries. The indexes you name are defined automatically. y You can code constant values for a table that uses indexes just as you do for a table that uses subscripts.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 25

Basic skills for working with indexes (continued)


y You can initialize a table that uses indexes by including a alue clause in the table definition or by using the Initialize statement in the Procedure Division. y You can refer to table entries using indexes, occurrence numbers, or relative indexes. You can use these techniques to refer to a group field or an individual field.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 26

Cod fo lo di

-l

l t bl

i d

WORKING-STORAGE SECTION. . . 01 SWITCHES. 05 PTABLE-EOF-SWITCH 88 PTABLE-EOF . . 01 PRICE-TABLE 05 PRICE-GROUP 10 10 01 ITEM-NUMBER ITEM-PRICE

PIC X

VALUE "N". VALUE "Y".

VALUE ZERO. OCCURS 16 TIMES INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.

PRICE-TABLE-RECORD. 05 PT-ITEM-NUMBER 05 PT-ITEM-PRICE . .

PIC 9(3). PIC S99V99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 27

Code for loading a one level ta le using an index (continued)


PROCEDURE DIVISION. . 100-LOAD-PRICE-TABLE. PERFORM WITH TEST AFTER VARYING PRICE-TABLE-INDEX FROM 1 BY 1 UNTIL PTABLE-EOF OR PRICE-TABLE-INDEX = 16 PERFORM 110-READ-PRICE-TABLE-RECORD IF NOT PTABLE-EOF MOVE PT-ITEM-NUMBER TO ITEM-NUMBER (PRICE-TABLE-INDEX) MOVE PT-ITEM-PRICE TO ITEM-PRICE (PRICE-TABLE-INDEX). 110-READ-PRICE-TABLE-RECORD. READ PTABLE RECORD INTO PRICE-TABLE-RECORD AT END SET PTABLE-EOF TO TRUE. . .
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 28

T e syntax of t e Set statement for setting index values


identifier - 2 identifier - 1 SET TO index - name - 2 index - name - 1 literal

T e syntax of t e Set statement for adjusting an index value


UP BY identifier SET index - name DOWN BY literal

T e syntax for defining an index data item


level-number data-name [ USAGE IS] INDEX

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 29

Set statement examples


A Set statement t at sets an index to t e value of an integer data item
SET MONTH-INDEX TO CURRENT-MONTH.

A Set statement t at sets an index to a literal value


IF MONTH-INDEX = 13 SET MONTH-INDEX TO 1.

A Set statement t at increases an index y one occurrence


SET MONTH-INDEX UP BY 1.

A Set statement t at sets an index data item to t e value of an index


05 WORK-INDEX INDEX. . SET WORK-INDEX TO MONTH-INDEX.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 30

Coding rules for t e Set statement


y The et statement can be used ith an index or an index data item. y An index data item is a ield thats de ined ith Index usage. It contains a displacement value ust like an index. y The compiler determines the size o an index data item, so icture and Value clauses arent allo ed on its de inition. y You can use the et statement to set an index, an index data item, or an integer ield to the value o another index or index data item. y You can also use the et statement to set an index or index data item to a literal value that represents an occurrence number or an integer data item that contains an occurrence number. y You can also use the et statement to increase or decrease the value o an index or an index data item.
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 31

T e syntax of t e Searc statement for a sequential searc


identifier - 2 SEARCH identifier - 1 VARYING index - name ? AT END imperative- statement - 1 A WHEN condition - 1 imperative - statement - 2 NEXT SENTENCE

? END - SEARCH A

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 32

A Search statement that tests for an equals condition


SET PRICE-TABLE-INDEX TO 1. SEARCH PRICE-GROUP AT END MOVE "N" TO ITEM-FOUND-SWITCH WHEN ITEM-NUMBER (PRICE-TABLE-INDEX) = TR-ITEM-NO SET ITEM-FOUND TO TRUE.

A Search statement that tests for a less-than-orequal-to condition


SET AGE-INDEX TO 1. SEARCH AGE-GROUP AT END MOVE "N" TO AGE-GROUP-FOUND-SWITCH WHEN AR-AGE <= HIGH-AGE (AGE-INDEX) SET AGE-GROUP-FOUND TO TRUE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 33

Code that tests for multi le matching entries


SET PRICE-TABLE-INDEX TO 1. MOVE "N" TO PTABLE-EOF-SWITCH. PERFORM UNTIL PTABLE-EOF SEARCH PRICE-GROUP AT END SET PTABLE-EOF TO TRUE WHEN ITEM-PRICE (PRICE-TABLE-INDEX) = ITEM-PRICE DISPLAY "ITEM NUMBER: " ITEM-NUMBER (PRICE-TABLE-INDEX) END-SEARCH SET PRICE-TABLE-INDEX UP BY 1 END-PERFORM.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 34

How to perform a sequential searc


y The earch statement per orms a sequential search o the speci ied table starting ith the entry identi ied by the current index value. y When the end o the table is reached, the At End clause is executed. y You can code one or more When clauses on a earch statement to test or speci ic conditions. When any o the conditions are satis ied, the earch statement ends. y The Varying clause names the index you search.

ant to use or the

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.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 35

T e syntax of a ta le definition for a inary searc


level - number data - name - 1 OCCURS integer TIMES ASCENDING KEY IS data - name - 2 DESCENDING INDEXED BY

_index - name - 1 a ...

T e syntax of t e Searc statement for a inary searc


SEARCH ALL identifier ? AT END im erative - tatement - 1 A WHEN equal nditi n - 1

? AND

equal -

nditi n - 2 A ...

im erative - tatement - 2 NEXT SENTENCE ? END - SEARCH A

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 36

Code for performing a binary search


01 PRICE-TABLE. 05 PRICE-GROUP OCCURS 16 TIMES ASCENDING KEY IS ITEM-NUMBER INDEXED BY PRICE-TABLE-INDEX. PIC 9(3). PIC S99V99.

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.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 37

How to perform a inary searc


y With a binary search, an entry near the middle o the table is compared ith the desired entry. ased on this comparison, the search continues in the irst or second hal o the table. This continues until the entry is ound or the entire table is searched. y To per orm a binary search, you use the earch All statement. The When clause o this statement speci ies the condition that satis ies and ends the search. y I none o the entries in the table satis y the search condition, the At End clause is executed. y To use a binary search, the table entries must be in sequence by the ield that ill be used or the search, called the key field. y The key ield is identi ied by the Key clause or the table entry. This ield should be unique and can be in either ascending or descending sequence.
Murachs Mainframe COBOL 2004, Mike Murach & Associates, Inc. Chapter 10, Slide 38

Cod for i d

r hi

two-l

l t bl

WORKING-STORAGE SECTION. . . 01 WORK-FIELDS. 05 POLICY-RATE 01 RATE-TABLE. 05 AGE-GROUP 10 10 HIGH-AGE CLASS-GROUP 15 15 01

PIC S99V99.

CLASS-NUMBER INSURANCE -RATE

OCCURS 6 TIMES INDEXED BY AGE-INDEX. PIC 99. OCCURS 4 TIMES INDEXED BY CLASS -INDEX. PIC 99. PIC S99V99.

APPLICANT-RECORD. 05 AR-AGE 05 AR-CLASS . .

PIC 99. PIC 99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 39

Code for searc ing a two-level ta le using indexes (continued)


PROCEDURE DIVISION. . . 350-SEARCH-RATE-TABLE. PERFORM 360-SEARCH-AGE-GROUP. IF AGE-GROUP-FOUND PERFORM 370-SEARCH-CLASS-GROUP IF CLASS-FOUND MOVE INSURANCE-RATE (AGE-INDEX CLASS-INDEX) TO POLICY -RATE. 360-SEARCH-AGE-GROUP. SET AGE-INDEX TO 1. SEARCH AGE-GROUP AT END MOVE "N" TO AGE -GROUP-FOUND-SWITCH WHEN AR-AGE <= HIGH-AGE (AGE-INDEX) SET AGE-GROUP-FOUND TO TRUE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 40

Code for searc ing a two-level ta le using indexes (continued)


370-SEARCH-CLASS-GROUP. SET CLASS-INDEX TO 1. SEARCH CLASS-GROUP AT END MOVE "N" TO CLASS-FOUND-SWITCH WHEN CLASS-NUMBER (AGE-INDEX CLASS-INDEX) = AR-CLASS SET CLASS-FOUND TO TRUE. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 41

T e syntax of a ta le definition for a varia le-lengt ta le


level - number data - name - 1 OCCURS integer - 1 TO integer - 2 TIMES DEPENDING ON data - name - 2
ASCENDING KEY IS data - name - 3 DESCENDING ? INDEXED BY _index - name - 1 a ...A

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 42

A ta le definition for a varia le-lengt ta le


01 PRICE-TABLE. 05 PRICE-GROUP 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.

10 10

ITEM-NUMBER ITEM-PRICE

T e definition of t e Depending
05 PT-ENTRY-COUNT PIC S9(3).

n field

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 43

How to define a variable-length table


y The Occurs clause for a variable-length table indicates the minimum and maximum number of times the group or field occurs in working storage. y The Depending On clause names a field that contains the actual number of occurrences. This field must be defined as an integer in working storage. y Before you can refer to an entry in a variable-length table, the Depending On field must be set to a value that falls within the range specified by the Occurs clause.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 44

Code for loading a varia le-length ta le


WORKING-STORAGE SECTION. . 01 SWITCHES. 05 PTABLE-EOF-SWITCH 88 PTABLE-EOF . 01 COUNT-FIELDS. 05 PT-ENTRY-COUNT 01 PRICE-TABLE. 05 PRICE-GROUP PIC X VALUE "N". VALUE "Y".

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.

PRICE-TABLE-RECORD. 05 PT-ITEM-NUMBER 05 PT-ITEM-PRICE . .

PIC 9(3). PIC S99V99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 45

Code for loading a variable-length table (continued)


PROCEDURE DIVISION. . 100-LOAD-PRICE-TABLE. PERFORM WITH TEST AFTER VARYING PRICE-TABLE-INDEX FROM 1 BY 1 UNTIL PTABLE-EOF OR PRICE-TABLE-INDEX = 100 PERFORM 110-READ-PRICE-TABLE-RECORD IF NOT PTABLE-EOF MOVE PT-ITEM-NUMBER TO ITEM-NUMBER (PRICE-TABLE-INDEX) MOVE PT-ITEM-PRICE TO ITEM-PRICE (PRICE-TABLE-INDEX) ELSE SET PRICE-TABLE-INDEX DOWN BY 1 SET PT-ENTRY-COUNT TO PRICE-TABLE-INDEX.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 46

Code for loading a variable-length table (continued)


110-READ-PRICE-TABLE-RECORD. READ PTABLE RECORD INTO PRICE-TABLE-RECORD AT END SET PTABLE-EOF TO TRUE. . .

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 47

Another way of loadin a variable len th table


WORKING-STORAGE SECTION. . . 01 COUNT-FIELDS. 05 PT-ENTRY-COUNT 01 PRICE-TABLE. 05 PRICE-GROUP 10 10 01 ITEM-NUMBER ITEM-PRICE

INDEX.

OCCURS 100 TIMES INDEXED BY PRICE -TABLE-INDEX. PIC 9(3). PIC S99V99.

PRICE-TABLE-RECORD. 05 PT-ITEM-NUMBER 05 PT-ITEM-PRICE . .

PIC 9(3). PIC S99V99.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 48

Another way of loading a variable-length table (continued)


PROCEDURE DIVISION. . 100-LOAD-PRICE-TABLE. PERFORM WITH TEST AFTER VARYING PRICE-TABLE-INDEX FROM 1 BY 1 UNTIL PTABLE-EOF OR PRICE-TABLE-INDEX = 100 PERFORM 110-READ-PRICE-TABLE-RECORD IF NOT PTABLE-EOF MOVE PT-ITEM-NUMBER TO ITEM-NUMBER (PRICE-TABLE-INDEX) MOVE PT-ITEM-PRICE TO ITEM-PRICE (PRICE-TABLE-INDEX) ELSE SET PRICE-TABLE-INDEX DOWN BY 1 SET PT-ENTRY-COUNT TO PRICE-TABLE-INDEX. 110-READ-PRICE-TABLE-RECORD. READ PTABLE RECORD INTO PRICE-TABLE-RECORD AT END SET PTABLE-EOF TO TRUE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 49

A Search statement f r a variable-length table


SET PRICE-TABLE-INDEX TO 1. SEARCH PRICE-GROUP AT END MOVE "N" TO PRICE-FOUND-S ITCH HEN PRICE-TABLE-INDEX > PT-ENTRY-COUNT MOVE "N" TO PRICE-FOUND-S ITCH HEN ITEM-NUMBER (PRICE-TABLE-INDEX) = TR-ITEM-NO MOVE ITEM-PRICE (PRICE-TABLE-INDEX) TO UNIT-PRICE SET PRICE-FOUND TO TRUE.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 50

T e expanded syntax of t e Perform statement


PERFORM

? procedure - name A

BEFORE WITH TEST AFTER

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.

WORKING-STORAGE SECTION. . 01 WORK-FIELDS. 05 RATE-TOTAL . 01 RATE-TABLE. 05 AGE-GROUP 10 10 HIGH-AGE CLASS-GROUP 15 15

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

Coding rules for the Perform Varying statement


y You can use the After clause of the Perform arying statement to vary up to six indexes or subscripts in addition to the index or subscript varied by the arying clause. y In most cases, youll specify the indexes or subscripts in sequence from the highest to the lowest level for the table.

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 53

Functions you can use with tables


Function name
M AN M DIAN S ANDA D D A IANC ANG MID ANG MAX MIN O D MAX O D MIN S M S N AL IA ION

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

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Examples of using intrinsic functions in tables


The table definition
01 RATE-TABLE. 05 AGE-GROUP 10 10 HIGH-AGE CLASS-GROUP 15 15 CLASS -NUMBER INSURANCE -RATE OCCURS 6 TIMES INDEXED BY AGE -INDEX. PIC 99. OCCURS 4 TIMES INDEXED -BY CLASS-INDEX. PIC 99. PIC S99V99.

A function that calculates the mean of all the insurance rates


FUNCTION MEAN (INSURANCE -RATE (ALL ALL))

A function that calculates the mean of all the insurance rates in the first age group
FUNCTION MEAN (INSURANCE -RATE (1 ALL))

Murachs Mainframe COBOL

2004, Mike Murach & Associates, Inc.

Chapter 10, Slide 55

You might also like