enterprise cobol concepts

260
Enterprise COBOL Concepts Dr. David Woolbright [email protected] 2013

Upload: stamos

Post on 11-Jan-2016

23 views

Category:

Documents


1 download

DESCRIPTION

Enterprise COBOL Concepts. Dr. David Woolbright [email protected] 2013. Video Link. Watch the Introductory video lesson here. COBOL?. Co mmon B usiness O riented L anguage Billions of lines of existing code with more added each year Designed for business processing - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Enterprise COBOL Concepts

Enterprise COBOL Concepts

Dr. David Woolbright

[email protected]

2013

Page 2: Enterprise COBOL Concepts

Video Link

• Watch the Introductory video lesson here

Page 3: Enterprise COBOL Concepts

COBOL?

• Common Business Oriented Language• Billions of lines of existing code with more added

each year• Designed for business processing • Great compilers• Cobol programs execute quickly• Relatively simple to learn• Not your father’s or mother’s COBOL - The

language keeps evolving (XML, OO)

Page 4: Enterprise COBOL Concepts

Enterprise Cobol for z\OS

IBM COBOL: • http://www-01.ibm.com/software/awdtools/cobol/zos/

Languagage Reference• http://publibfp.boulder.ibm.com/epubs/pdf/igy3lr50.pdf

Programming Guide• http://publibfp.boulder.ibm.com/epubs/pdf/igy3pg50.pdf

Page 5: Enterprise COBOL Concepts

Program Organization

• Program – Organized like a book

• Division – Identification, Environment, Data, Procedure

• Section• Paragraph• Sentence• Clause• Phrase• Word

Page 6: Enterprise COBOL Concepts

Grammatical Hierarchy• The grammatical hierarchy follows this form: • Identification division

– Paragraphs • Entries

– Clauses

• Environment division – Sections

• Paragraphs – Entries

» Clauses » Phrases

• Data division – Sections

• Entries – Clauses

» Phrases

• Procedure division – Sections

• Paragraphs – Sentences

» Statements » Phrases

Page 7: Enterprise COBOL Concepts

Coding Rules

• Cols 1-6 – left blank. Editor fills in with sequence numbers• Col 7 – Usually blank,* means comment line, - is continuation, D for debugging lines• Cols 8-11 – “A” margin or Area A• Cols 12-72 – “B” margin or Area B• Cols 73-80 – unused• 1 2 3 4 5 6|7| 8 9 10 11|12 13 …71 72 |

Seq Nos | | Area A | Area B |

Page 8: Enterprise COBOL Concepts

Continuation of Statements

• Statements can be continued on the next line in Area B

Page 9: Enterprise COBOL Concepts

Continuation of Literals

• Continue the constant through column 71• Put a “-” in column 7• Continue constant with a ‘ OR “• Continue constant in area B

Page 10: Enterprise COBOL Concepts

Things That Go in Area A

Area A items:• Division headers • Section headers • Paragraph headers or paragraph names • Level indicators or level-numbers (01 and 77) • DECLARATIVES and END DECLARATIVES • End program, end class, and end method markers

Page 11: Enterprise COBOL Concepts

Things That Go in Area B

Area B items:• Entries, sentences, statements, and clauses • Continuation lines

Page 12: Enterprise COBOL Concepts

Things That Go in A or B

• Level-numbers

• Comment lines

• Compiler-directing statements

• Debugging lines

• Pseudo-text

Page 13: Enterprise COBOL Concepts

Preparing a Program

Your Cobol

Program

CoboL Compiler

Object Module

Linkage Editor

Load Module

Page 14: Enterprise COBOL Concepts

Things You Should Try

• You will need to create a PDS (Partitioned Data Set) to hold the programs (members) you create. You can watch a video about how to create a PDS here.

• You will also need a load library where the linkage editor can place the executable programs (load modules) you create. Here is a video that describes how to build a load library.

Page 15: Enterprise COBOL Concepts

Things You Should Try

• When you’re beginning to learn a new language, it’s a good idea to type in a few programs from scratch. Here is a Hello, World program you should create in your PDS. And here is a video of how to create the program.

• Here’s a link to some JCL that can be modified to compile, link, and run a COBOL program on your system.

Page 16: Enterprise COBOL Concepts

Things You Should Try

• Here is a video that discusses how to change the JCL to fit your situation.

Page 17: Enterprise COBOL Concepts

Video Link

• Watch the Video lesson #2 here

Page 18: Enterprise COBOL Concepts

Structure of a Program

Page 19: Enterprise COBOL Concepts

IDENTIFICATION DIVISION

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. AUTHOR. JOE SMITH. INSTALLATION. TSYS. DATE-WRITTEN. 12/03/2011. DATE-COMPILED. 12/03/2011.

• Only PROGRAM-ID is required• Some interesting parms can be coded on the PROGRAM-ID

Page 20: Enterprise COBOL Concepts

ENVIRONMENT DIVISION

This division connects external DD file names with internal file names.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT MSTRFILE ASSIGN TO MSTRFILE SELECT CUSTOMER-FILE

ASSIGN TO CUSTMAST ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS COSTOMER-KEY FILE STATUS IS CUSTOMER-FILE-STATUS.

Internal File Name

External DD File Name

Page 21: Enterprise COBOL Concepts

DATA DIVISION

• Used to create variables and constant fields• Only three data types

– numeric PIC 99999.– alphanumeric (text/string) PIC XXX.– alphabetic PIC AAA.

• Level numbers indicate subordination of fields. Use levels 01-49

• Alphabetic is seldom used

Page 22: Enterprise COBOL Concepts

Level Numbers

• Group item – a subdivided field• Elementary item – a non-subdivided field• 01 – Group or independent item• Higher numbers indicate subordinate fields

Page 23: Enterprise COBOL Concepts

Level Numbers

• 66, 77, 88 have special significance• 66 – Used to rename (no longer used)• 77 – An independent item (choose 01)• 88 – Condition name

Page 24: Enterprise COBOL Concepts

Level Numbers

01 XXX.

05 YYY.

10 AAA PIC X.

10 BBB PIC X.

05 ZZZ PIC X(20).

77 AAA PIC 999V99.

Page 25: Enterprise COBOL Concepts

Condition Names

• 01 TRAN-CODE PIC X. 88 GOOD VALUE ‘G’. 88 BAD VALUE ‘B’. 88 INDIFFERENT VALUE ‘I’.…SET GOOD TO TRUE…IF (GOOD) …

Equivalent to MOVE ‘G’ TO TRAN-CODE

Equivalent to IF TRAN-CODE = ‘G’

Page 26: Enterprise COBOL Concepts

Level 88

Page 27: Enterprise COBOL Concepts

Condition Names

Page 28: Enterprise COBOL Concepts

Picture Clauses

• Picture clause values usually use 9, X, V, S, A

• 9 – a decimal digit

• X – any alphanumeric character

• V – an implied decimal point

• S – a sign

• A – A-Z, and blank

Page 29: Enterprise COBOL Concepts

Picture Clauses

• PIC 9(6) • PIC 9(6)V99 • PIC 999999V99 • PICTURE X(10) • PIC XXXXXXXXXX • PIC S9(4)V9(4)• PIC S9999V9999 • PIC 9(18)

Page 30: Enterprise COBOL Concepts

Numeric Edited Fields

• XXXBXXBXXXX• 99/99/99• ZZ,ZZZ.99DB• ***,***.99• ----.99• $$$9.99• 99999.99

Page 31: Enterprise COBOL Concepts

USAGE Clause

• Specifies the format in which data is stored in memory

• Normally, the phrase “USAGE IS” is omitted

01 COST USAGE IS PACKED-DECIMAL PIC S9(5).

01 COST PACKED-DECIMAL PIC S9(5).

01 FIRST-NAME USAGE IS DISPLAY PIC X(20).

01 FIRST-NAME PIC X(20).

Page 32: Enterprise COBOL Concepts

DATA DIVISION

We define data used in input-output operations.

FILE SECTION.FD CUSTOMER-FILE.01 CUSTOMER-MASTER. 05 CUST-NUM PIC 9(2). 05 CUST-FNAME PIC X(20). 05 CUST-LNAME PIC X(20).FD SALES-REPORT.01 REPORT-AREA PIC X(132).

Page 33: Enterprise COBOL Concepts

Data Formats

Older terms: Modern terms:• COMPUTATIONAL BINARY• COMP BINARY• COMP-1 FLOATING POINT• COMP-2 FLOATING POINT• COMP-3 PACKED-DECIMAL• COMP-4 BINARY• COMP-5 BINARY (NATIVE)

05 XDATA PIC S9(5) PACKED-DECIMAL.

05 YDATA PIC S9(4) BINARY.

Page 34: Enterprise COBOL Concepts

EBCDIC

• EBCDIC is an IBM format for storing alphanumeric characters

A - X’C1’ J - X’D1’ B - X’C2’ K - X’D2’ S – X’E2’ C - X’C3’ L - X’D3’ T – X’E3’ D - X’C4’ M - X’D4’ U – X’E4’ E - X’C5’ N - X’D5’ V – X’E5’ F - X’C6’ O - X’D6’ W – X’E6’ G - X’C7’ P - X’D7’ X – X’E7’ H - X’C8’ Q - X’D8’ Y – X’E8’ I - X’C9’ R - X’D9’ Z – X’E9’

Page 35: Enterprise COBOL Concepts

EBCDIC

• EBCDIC is an IBM format for storing alphanumeric characters

0 - X’F0’ SPACE – X’40’ 1 – X’F1’ . - X’4B’ 2 - X’F2’ , - X’6B’ 3 – X’F3’ * - X’5C’ 4 - X’F4’ - - X’60’ 5 – X’F5’ 6 - X’F6’ 7 – X’F7’ 8 – X’F8’ 9 – X’F9’

Page 36: Enterprise COBOL Concepts

BINARY DATA

• Stored in 2’s Complement format

• Leftmost bit is a sign ( 0 +, 1 - )

• If the number is positive, interpret it as plain binary 01011 = 8 + 2 + 1 = + 11

• If the number is negative, compute the complement – Invert. (Change all 1’s to 0’s and 0’s to 1’s.) Add 1. The result is the additive complement

Page 37: Enterprise COBOL Concepts

BINARY DATA

• 10011 is a negative number. Inverting we have 01100. Adding 1 we have 01100 + 1 = 01101. This is a positive

number. 01101 8 + 4 + 1 = 13, so the original number is -13.

Page 38: Enterprise COBOL Concepts

BINARY DATA

• Declaring a data field as BINARY causes the data to be stored in 2’s complement format.

• 01 X-FIELD PIC S9(4) BINARY VALUE -1.

• X-FIELD will contain

1111111111111111 = X’FFFF’.• Binary data is processed in a register

Page 39: Enterprise COBOL Concepts

PACKED-DECIMAL DATA• Defining a field to be PACKED-DECIMAL or Computational-3

causes the data to be stored internally in a packed decimal format.

• There are 2 decimal digits stored in each byte. A sign is stored in the rightmost “nibble”. (C +, D -)

• Y-FIELD PIC S999 VALUE -23 PACKED-DECIMAL. produces a 2 byte field containing X’023D’

• Most business arithmetic occurs in packed decimal.

Page 40: Enterprise COBOL Concepts

Packed No-Sign Data

• Packed no-sign data is a non-native data type that was created to save space on a disk when storing dates

• If the digits in a date like 10/23/89 were stored in a packed field, the field would require 4 bytes: 01|02|38|9C

• By removing the sign, the date fits in 3 bytes: 10|23|89

Page 41: Enterprise COBOL Concepts

Packed No-Sign Data

• These type fields require special handling in Cobol

• Program PKNOSIGN illustrates how a displayable date can be recovered from a packed no-sign field.

Page 42: Enterprise COBOL Concepts

ZONED-DECIMAL DATA

• A numeric field which is described as DISPLAY, or in which the usage clause is omitted, is stored in a zoned decimal format.

• In zoned decimal, each digit takes up one byte, and a sign is stored in the zone portion of the rightmost byte of the field.

• Z-FIELD PIC S999 VALUE -32 produces a 3 byte field containing X’F0F3D2’.

Page 43: Enterprise COBOL Concepts

ZONED-DECIMAL DATA

• Z-FIELD PIC S999 VALUE -32. produces a 3 byte field containing X’F0F3D2’.• Z-FIELD PIC S999 VALUE 32. produces a 3 byte field containing X’F0F3C2’.• W-FIELD PIC 999 VALUE 0. MOVE -32 TO W-FIELD produces a 3 byte field containing X’F0F3C2’.

Page 44: Enterprise COBOL Concepts

DATA DIVISION

Define the data needed for internal processing in the WORKING-STORAGE SECTION.

Storage is statically allocated and exists for the life of the run unit.

WORKING-STORAGE SECTION.01 TOTAL-FIELDS. 05 CUST-TOTAL PIC S9(7)V99 VALUE 0. 05 COST-TOTAL PIC S9(7)V99 VALUE 0.01 DATE-AND-TIME. 05 CD-YEAR PIC 9999. 05 CD-MONTH PIC 99.

Page 45: Enterprise COBOL Concepts

DATA RELATIONSHIPS

BINARY

PACKED-DECIMAL

CHARACTER or

ALPHANUMERIC

ZONED-DECIMAL

Page 46: Enterprise COBOL Concepts

DATA DIVISION

• Describe data that exists in another program, or storage you want to associate with a symbolic name in the LINKAGE SECTION.

LINKAGE SECTION.

01 LK-DATA-AREA

05 NAME PIC X(40).

05 AGE PIC 999.

Page 47: Enterprise COBOL Concepts

DATA DIVISION

The LOCAL-STORAGE SECTION is used to have storage allocated each time a program is entered, and deallocated on return from the program. Used for compatibility with C or Java.

LOCAL-STORAGE SECTION.

01 CUST-NO PIC X(3).

01 COST PIC 9(5)V99.

Page 48: Enterprise COBOL Concepts

Initialization of Storage

• WORKING-STORAGE for programs is allocated at the start of the run unit.

• Any data items with VALUE clauses are initialized to the appropriate value at that time.

Page 49: Enterprise COBOL Concepts

Initialization of Storage

• For the duration of the run unit, WORKING-STORAGE items persist in their last-used state. Exceptions are:

1) A program with INITIAL specified in the PROGRAM-ID paragraph In this case, WORKING-STORAGE data items are reinitialized each time the program is entered.

IDENTIFICATION DIVISION. PROGRAM-ID. MAIN IS INITIAL. ...

Page 50: Enterprise COBOL Concepts

Initialization of Storage

• For the duration of the run unit, WORKING-STORAGE items persist in their last-used state. Exceptions are:

2) A subprogram that is dynamically called and then canceled In this case, WORKING-STORAGE data items are reinitialized on the first reentry into the program following the CANCEL.

MOVE ‘PROG23’ TO PROGID CALL PROGID CANCEL PROGID CALL PROGID

Page 51: Enterprise COBOL Concepts

Group and Data Items

01 Customer-Record. 05 Customer-Name. 10 Last-Name Pic x(17). 10 Filler Pic x. 10 Initials Pic xx. 05 Part-Order. 10 Part-Name Pic x(15). 10 Part-Color Pic x(10).

Page 52: Enterprise COBOL Concepts

REDEFINES

01 MONTH-NAMES.

05 STRING-1 PIC X(15)

VALUE “JANFEBMARAPRMAY’.

05 MONTH REDEFINES STRING-1

OCCURS 5 TIMES PIC XXX.

MOVE MONTH(3) TO MONTH-OUT

Page 53: Enterprise COBOL Concepts

REDEFINES

05 AMOUNT PIC ZZ9.9-. 05 AMOUNTX REDEFINES AMOUNT PIC X(6).

10 XFIELD PIC 9(5). 10 YFIELD REDEFINES XFIELD. 20 A PIC X(3). 20 B PIC X(2).

Page 54: Enterprise COBOL Concepts

Literals

• String Literals enclosed in quotes (single or double)

MOVE "INVALID" To CUST-NAME

• Numeric literals without quotes

MOVE 19 TO CUST-AGE

Page 55: Enterprise COBOL Concepts

Literals

• Hexadecimal literals with X’…’

MOVE X’AF3B’ TO CUST-CODE

Page 56: Enterprise COBOL Concepts

Constants

• A constant is a data item that has only one value and it can never change

• Unfortunately, COBOL does not define a construct specifically for constants

• Moral: All values are subject to change

Data Division.

01 Report-Header pic x(50)

value "Company Report".

01 Interest pic 9v9999

value 1.0265.

Page 57: Enterprise COBOL Concepts

Figurative Constants

There are some figurative constants supplied by the language:

• ZERO - an appropriate form of 0• SPACE - x’40’• HIGH-VALUES - binary 1’s • LOW-VALUES - binary 0’s• QUOTE - a single quote• NULL - binary 0’s used for pointers• ALL - Technically not a figurative constant: X PIC X(5) VALUE ALL ‘3’.

Page 58: Enterprise COBOL Concepts

Tables (Arrays)

• A table is a set of logically consecutive data items that you define in the DATA DIVISION by using the OCCURS clause.

01 TABLE. 05 TABLE-ENTRY OCCURS 10 TIMES. 10 NUM PIC 99. 10 NAME PIC X(30). 10 ITEM PIC X(5) OCCURS 3 TIMES.

Page 59: Enterprise COBOL Concepts

Referencing a Table01 TABLE. 05 TABLE-ENTRY OCCURS 10 TIMES. 10 NUM PIC 99. 10 NAME PIC X(30). 10 ITEM PIC X(5) OCCURS 3 TIMES.

COBOL tables are 1-indexed Some valid References with subscripts:

TABLE-ENTRY(SUB) TABLE NUM(SUB) NAME(SUB) ITEM(SUB1,SUB2)

Page 60: Enterprise COBOL Concepts

Subscripts vs Indexes

• Subscripts are defined separately from the table definitions.

01 MYTABLE. 05 ITEM PIC X(3) OCCURS 10 TIMES. 01 I PIC 9(4) BINARY. ... MOVE 1 TO I MOVE “ABC” TO ITEM(I)

• Subscripts are numeric fields – choose BINARY fields for efficiency, although packed and zoned fields also work

Page 61: Enterprise COBOL Concepts

Subscripts vs Indexes

• Subscripts can be easily printed 01 MYTABLE. 05 ITEM PIC X(3) OCCURS 10 TIMES. 01 I PIC 9(4) BINARY. ... MOVE 1 TO I MOVE “ABC” TO ITEM(I) DISPLAY I

Page 62: Enterprise COBOL Concepts

Subscripts vs Indexes

• Subscripts represent an occurrence number, 1 is the first occurrence, 2 is the second, …

01 MYTABLE. 05 ITEM PIC X(3) OCCURS 10 TIMES. 01 I PIC 9(4) BINARY. ... PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 DISPLAY ITEM(I) END-PERFORM

Page 63: Enterprise COBOL Concepts

Subscript Program

Page 64: Enterprise COBOL Concepts

Subscripts vs Indexes

• Indexes are created when a table is defined 01 MYTABLE.

10 LETTERVALS PIC X(10) VALUE 'ABCDEFGHIJ'. 10 LETTER REDEFINES LETTERVALS PIC X(1) OCCURS 10 TIMES INDEXED BY I.

• Indexes are manipulated with SET statements and automatically altered by PERFORM statements

SET I TO 1 MOVE LETTER(I) TO CHAROUT

Page 65: Enterprise COBOL Concepts

Subscripts vs Indexes

• Indexes are generally more efficient than subscripts

• Indexes represent offsets from the beginning of the table SET I TO 1 Causes I to have the binary value 0 internally.

• It takes a bit of work to print them

Page 66: Enterprise COBOL Concepts

Index Program

Page 67: Enterprise COBOL Concepts

Tables with Two Dimensions01 TABLE. 05 TABLE-ENTRY OCCURS 10 TIMES INDEXED BY SUB1. 10 NUM PIC 99. 10 NAME PIC X(30). 10 ITEM PIC X(5) OCCURS 3 TIMES INDEXED BY SUB2.

Valid References with subscripts: TABLE-ENTRY(SUB1) TABLE NUM(SUB1) NAME(SUB1) ITEM(SUB1,SUB2)

Page 68: Enterprise COBOL Concepts

Subscripts and Indexes

• In a two-dimensional table, the two subscripts correspond to the row and column numbers.

• In a three-dimensional table, the three subscripts correspond to the depth, row, and column numbers.

• Indexes use address computation to efficiently reference items in a table.

Page 69: Enterprise COBOL Concepts

A Testy Program

• This program displays “You Chose Paper”.

• Why?

Page 70: Enterprise COBOL Concepts

PROCEDURE DIVISION

• The PROCEDURE DIVISION is where you code the executable statements in your COBOL program

• Divided into Paragraphs (terminated with periods): 100-MAIN. DISPLAY “HELLO…” PERFORM 200-SUB GOBACK . 200-SUB. DISPLAY “…WORLD!” .

Page 71: Enterprise COBOL Concepts

The Evils of Coding Periods

• Periods have proven troublesome in the procedure division, and a new style of coding has evolved to deal with them

• The period is a statement terminator but we want to think of it as a paragraph terminator only

• We will code in a “period-less” style

• Only use periods to end a paragraph name

Page 72: Enterprise COBOL Concepts

PROCEDURE DIVISION

• To resolve ambiguity caused by not using periods, we will use statement delimiters:

END-IF

END-PERFORM

END-COMPUTE

...

Page 73: Enterprise COBOL Concepts

DISPLAY

Page 74: Enterprise COBOL Concepts

DISPLAY

• Handy for debugging and simple report creation

• DISPLAY X Y Z• DISPLAY "A = " A• Data is written to SYSOUT

Page 75: Enterprise COBOL Concepts

MOVE

Page 76: Enterprise COBOL Concepts

MOVE STATEMENT

• Used to copy data from one field to another

• Example -

MOVE X-FIELD TO Y-FIELD Z-FIELD

• Data is copied from the sending field to the receiving field

Page 77: Enterprise COBOL Concepts

MOVE STATEMENT

• To move data from one field to another field, the two fields should be “compatible” but don’t have to be identically pictured

• Alphanumeric - PIC X(10)• Numeric - PIC 999v99 • Numeric-Edited - PIC 999.99-

Compatible moves: -Alphanumeric to Alphanumeric -Numeric to Numeric -Numeric to Numeric edited

Page 78: Enterprise COBOL Concepts

MOVE STATEMENT

• Compatible moves:

-Alphanumeric to Numeric if the sending field is an unsigned integer

-Alphanumeric to Numeric edited if the sending field is an unsigned integer

-Numeric to Alphanumeric if the sending field is an unsigned integer

Page 79: Enterprise COBOL Concepts

MOVE STATEMENT

• Compatible moves:

-Numeric edited fields can be sent to Numeric and Numeric edited fields – this causes a de-edit process to occur

-Numeric edited fields can be sent to Alphanumeric and

Alphanumeric edited fields – this causes a de-edit process to occur

Page 80: Enterprise COBOL Concepts

MOVE STATEMENT

• Moving data can cause data conversions to occur.

01 X PIC S9(4) BINARY.

01 Y PIC S9(5) PACKED-DECIMAL.

MOVE X TO Y

Page 81: Enterprise COBOL Concepts

MOVE STATEMENT

• If the receiving field is larger than the sending field, the receiving field is filled with leading 0’s in a numeric move:

01 X PIC S9(3) VALUE 123. 01 Y PIC S9(5) VALUE 0. MOVE X TO Y RESULT: Y = X’F0F0F1F2C3’

Page 82: Enterprise COBOL Concepts

MOVE STATEMENT

• If the receiving field is larger than the sending field, the receiving field is filled with trailing spaces in a alphanumeric move.

01 X PIC X(3) VALUE “ABC”. 01 Y PIC X(5) VALUE SPACES. MOVE X TO Y RESULT: Y = X’C1C2C34040’

Page 83: Enterprise COBOL Concepts

MOVE STATEMENT

• If the receiving field is smaller than the sending field, data will be truncated on the left for numeric moves and on the right for alphanumeric moves

01 X PIC S9(5) VALUE 12345. 01 Y PIC S9(3) VALUE 0. 01 A PIC X(5) VALUE ‘ABCDE’ 01 B PIC X(3) VALUE SPACES. MOVE X TO Y MOVE A TO B RESULT: Y = X’F3F4F5’ B = X’C1C2C3’

Page 84: Enterprise COBOL Concepts

Use Move for Assignment

01 WORK.

05 A-FIELD PIC X(3).

05 B-FIELD PIC S999V99.

MOVE “ABC” TO A-FIELD

MOVE 123.45 TO B-FIELD

MOVE LOW-VALUE TO WORK

Page 85: Enterprise COBOL Concepts

Assignment Can Cause Conversions

Consider the following move:

01 A PIC S999V99.01 B PIC ZZ9.99-.01 C PIC S9(5)V9999 PACKED-DECIMAL. MOVE A TO B Zoned to Numeric-edited MOVE A TO C Zoned to Packed-decimal

Page 86: Enterprise COBOL Concepts

MOVE CORRESPONDING

MOVE CORRESPONDING identifier-1 TO identifier-2

CORR• Usually a bad idea• Both identifiers must name group items.• Elementary items with the same name are moved.• 01 A-GROUP. 01 B-GROUP.

05 W PIC X(3). 05 W PIC X(3).

05 X PIC X(2). 05 X PIC X(2).

05 Y PIC 999. 05 Y PIC 999.

MOVE CORRESPONDING A-GROUP TO B-GROUP

• W to W, X to X, Y to Y

Page 87: Enterprise COBOL Concepts

MOVE CORRESPONDING

• Subordinate items must not be identified by the keyword FILLER

• No reference modification for either identifier• Subordinate items must not include a REDEFINES, RENAMES, OCCURS, INDEX or POINTER description

• 01 A-GROUP. 01 B-GROUP.

05 W PIC X(3). 05 P PIC X(3).

05 X PIC X(2). 05 X PIC X(2).

05 Y PIC 999. 05 W PIC 999.

MOVE CORRESPONDING A-GROUP TO B-GROUP

• W to W, X to X

Page 88: Enterprise COBOL Concepts

INITIALIZE

Page 89: Enterprise COBOL Concepts

INITIALIZE

• SPACE is the implied sending item for receiving items of category alphabetic, alphanumeric, alphanumeric-edited, DBCS, national, or national-edited.

• ZERO is the implied sending item for receiving items of category numeric or numeric-edited.

Page 90: Enterprise COBOL Concepts

INITIALIZE

01 PRICE-FIELDS.

05 UNIT-PRICE PIC 9(5)V9(2) PACKED-DECIMAL.

05 DISCOUNT PIC V9(2).

05 UNIT-CODE PIC XX.

05 SALES-PRICE PIC S9(4) BINARY.

. . .

INITIALIZE PRICE-FIELDS

Page 91: Enterprise COBOL Concepts

ADD

Page 92: Enterprise COBOL Concepts

ADD Semantics

• All identifiers or literals that precede the keyword TO are added together, and this sum is added to and stored in identifier-2. This process is repeated for each successive occurrence of identifier-2 in the left-to-right order in which identifier-2 is specified.

ADD X Y Z TO P Q

Before X=1, Y=2, Z=3, P=4, Q=6

After X=1, Y=2, Z=3, P=10, Q=12

Page 93: Enterprise COBOL Concepts

ADD EXAMPLES

• ADD X TO Y• ADD X Y Z TO P• ADD X Y TO P Q• ADD 1 TO Z• ADD X TO Y ROUNDED• ADD X TO Y ON SIZE ERROR DISPLAY “ADD ERROR” END-ADD

Page 94: Enterprise COBOL Concepts

ADD … GIVING

Page 95: Enterprise COBOL Concepts

ADD…GIVING Semantics

• All identifiers or literals that precede the keyword TO are added together, and this sum is added to identifier-2 to obtain a temporary sum. (Identifier-2 is unchanged)

• The the temporary sum is moved to identifier-3.

ADD X Y Z TO V GIVING P Before X=1, Y=2, Z=3, V=4, P=6 After X=1, Y=2, Z=3, V=4, P=10

Page 96: Enterprise COBOL Concepts

SUBTRACT

Page 97: Enterprise COBOL Concepts

SUBTRACT

• All identifiers or literals preceding the keyword FROM are added together and their sum is subtracted from and stored immediately in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left-to-right order in which identifier-2 is specified.

SUBTRACT X Y FROM P Q

Before: X=1,Y=2, P=3,Q=4

After: X=1,Y=2, P=0,Q=1

Page 98: Enterprise COBOL Concepts

SUBTRACT

Page 99: Enterprise COBOL Concepts

SUBTRACT Semantics

• All identifiers or literals preceding the keyword FROM are added together and their sum is subtracted from identifier-2 to obtain a temporary value which is moved to identifier-3.

SUBTRACT X Y FROM P GIVING Q

Before: X=1,Y=2,P=5,Q=6

After: X=1,Y=2,P=5,Q=2

Page 100: Enterprise COBOL Concepts

MULTIPLY

Page 101: Enterprise COBOL Concepts

MULTIPLY Semantics

• In format 1, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2. For each successive occurrence of identifier-2, the multiplication takes place in the left-to-right order in which identifier-2 is specified.

MULTIPLY X BY P Q

Before: X=2,P=4,Q=5

After: X=2,P=8,Q=10

Page 102: Enterprise COBOL Concepts

MULTIPLY

Page 103: Enterprise COBOL Concepts

MULTIPLY

• In format 2, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data items referenced by identifier-3. Identifier-2 is unchanged.

MULTIPLY X BY Y GIVING Z

Before: X=2, Y=3, Z=4

After: X=2, Y=3, Z=6

Page 104: Enterprise COBOL Concepts

DIVIDE

Page 105: Enterprise COBOL Concepts

DIVIDE

• In format 1, the value of identifier-1 or literal-1 is divided into the value of identifier-2, and the quotient is then stored in identifier-2. For each successive occurrence of identifier-2, the division takes place in the left-to-right order in which identifier-2 is specified.

DIVIDE X INTO Y Z

Before: X=3, Y=7, Z=12

After: X=3, Y=2, Z=4

Page 106: Enterprise COBOL Concepts

DIVIDE

Page 107: Enterprise COBOL Concepts

DIVIDE

• In format 2, the value of identifier-1 or literal-1 is divided into the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

DIVIDE X INTO Y GIVING Z

Before: X = 2, Y = 13, Z = 1

After: X = 2, Y = 13, Z = 6

Page 108: Enterprise COBOL Concepts

DIVIDE

Page 109: Enterprise COBOL Concepts

DIVIDE

• In format 3, the value of identifier-1 or literal-1 is divided by the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

DIVIDE X BY Y GIVING Z

Before: X = 10, Y = 3, Z = 1

After: X = 10, Y = 3, Z = 3

Page 110: Enterprise COBOL Concepts

DIVIDE

Page 111: Enterprise COBOL Concepts

DIVIDE

• In format 4, the value of identifier-1 or literal-1 is divided into identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

DIVIDE X INTO Y GIVING Z

REMAINDER R

Before: X = 2, Y = 9, Z = 8, R = 7

After: X = 2, Y = 9, Z = 4, R = 1

Page 112: Enterprise COBOL Concepts

COMPUTE

Page 113: Enterprise COBOL Concepts

COMPUTE

• COMPUTE can be used to initialize a numeric field• Usually reserved for nontrivial computations. For

simple computations choose ADD, SUBTRACT, MULTIPLY or DIVIDE

05 X PIC S9(4)V9 PACKED DECIMAL.

COMPUTE X ROUNDED = (A + B) / 2.3 ON SIZE ERROR DISPLAY “X WAS TRUNCATED”END-COMPUTE

Page 114: Enterprise COBOL Concepts

Arithmetic Operators

Parentheses provide precedence. Always parenthesize!((X + Y) * ( Z ** 3))

Operation Operator

+ Addition

- Subtraction

* Multiplication

/ Division

** Exponentiation

Page 115: Enterprise COBOL Concepts

Locate Mode I/O

Input Buffers Output Buffers

Your Program

Region

Page 116: Enterprise COBOL Concepts

Move Mode I/O

Input Buffers Output Buffers

READ MYFILE INTO MYREC

WRITE RECOUT FROM MYREC

Region

01 MYREC PIC X(80).

Page 117: Enterprise COBOL Concepts

QSAM FILE OPERATIONS

ENVIRONMENT DIVISION. INPUT-OUTPUT-SECTION. FILE-CONTROL. SELECT MY-INPUT-FILE ASSIGN TO MASTER FILE STATUS IS MAST-STAT. • MY-INPUT-FILE – Internal file name• MASTER – External DD name• MAST-STAT – A two byte PIC XX field in which the

operating system returns a status code. Consult IBM Language Reference page 315 for status code details.

Page 118: Enterprise COBOL Concepts

QSAM FILE OPERATIONS

ENVIRONMENT DIVISION. INPUT-OUTPUT-SECTION. FILE-CONTROL. SELECT MY-INPUT-FILE ASSIGN TO MASTER FILE STATUS IS MAST-STAT.

DATA DIVISION. FILE SECTION. FD MY-INPUT-FILE 01 RECORD-AREA PIC X(80).

Page 119: Enterprise COBOL Concepts

QSAM

• Queued Sequential Access Method• For input files, records are buffered when the file

is OPENed• For output, records are buffered before being

written• Records are processed from the beginning

record sequentially to the end of the file• Very efficient access method for sequential files• Sometimes referred to as “flat” files

Page 120: Enterprise COBOL Concepts

QSAM FILE OPERATIONS

• Every file must be OPENed before it can be processed.

• Opening a QSAM Input file queues records for subsequent read operations

• OPEN INPUT MY-INPUT-FILE• OPEN OUTPUT MY-OUTPUT-FILE

• Files should be closed when you have finished processing the records

• CLOSE MY-FILE

Page 121: Enterprise COBOL Concepts

OPEN

Page 122: Enterprise COBOL Concepts

CLOSE

Page 123: Enterprise COBOL Concepts

READ

Page 124: Enterprise COBOL Concepts

QSAM Input File Operations

• Remember: READ a file, WRITE a record. READ MY-INPUT-FILE

AT END MOVE ‘NO’ TO MORE-RECS

END-READ

• This is a locate-mode read and the most efficient way to read records

Page 125: Enterprise COBOL Concepts

File Reading

READ MY-INPUT-FILE INTO MY-REC

AT END MOVE ‘NO’ TO MORE-RECS

END-READ WRITE

• This is a move-mode read and the least efficient way to deliver records

Page 126: Enterprise COBOL Concepts

QSAM Output File Operations

• Write a record!• WRITE MY-RECORD (locate mode)• WRITE MY-RECORD FROM REC-BUFF END-WRITE (move mode)• CLOSE MY-OUTPUT-FILE• CLOSE MY-INPUT-FILE

Page 127: Enterprise COBOL Concepts

Sequential File Reading Pattern

READ MYFILE AT END MOVE ‘N’ TO MORE-RECSEND-READPERFORM UNTIL MORE-RECS = ‘N’ (process a record code) READ MYFILE AT END MOVE ‘N’ TO MORE-RECS END-READEND-PERFORM

PRIMING READ

CONTINUATION READ

Page 128: Enterprise COBOL Concepts

File Status Codes

• 00 – normal• 10 – end of file• 2x – invalid key• 3x – permanent i/o error• 4x – logic error• 9x – unsuccessful operation

Page 129: Enterprise COBOL Concepts

Exercise #1• Create a file of 80 byte records• Each record has 3 fields• AFIELD – ZONED DECIMAL with 4 DIGITS & 2 DECIMALS• BFIELD – PACKED DECIMAL with 7 DIGITS & 3 DECIMALS• CFIELD - PACKED DECIMAL with 7 DIGITS & 1 DECIMAL• Print a report with a column for each field and a column for the

computed value : (AFIELD + BFIELD)/ CFIELD Print the result with 2 decimals rounded. Total each column.

Page 130: Enterprise COBOL Concepts

FLOW OF CONTROL

• There is a theoretical result in Computer Science by two Italian mathematicians, Boehm and Jacopini, that states that only 3 control structures are required to write any program:

• Sequence - Do this, now do this, now do this, …• Selection - If something is true do this, else do that• Repetition – While something is true, do this• Practice has shown that being able to create procedures

is helpful in overcoming complexity, but they aren’t strictly necessary

• One implication of this result is that GO TO statements aren’t needed

Page 131: Enterprise COBOL Concepts

FLOW OF CONTROL

?

T F

F

T

Page 132: Enterprise COBOL Concepts

IF

Page 133: Enterprise COBOL Concepts

IF

• The condition is tested and either the true or false blocks are selected for execution

• Don’t use NEXT SENTENCE if you are using END-IF as the delimiter (and you should). Use of NEXT SENTENCE causes execution to continue with the next closest period, which is probably the end of the paragraph.

Page 134: Enterprise COBOL Concepts

IF Examples

IF X < Y ADD 1 TO X DISPLAY “AAA” ELSE DISPLAY “BBB” END-IF

IF X > Y DISPLAY “X WAS BIGGER” END-IF

Page 135: Enterprise COBOL Concepts

NESTED IFs

• Each ELSE is matched with the nearest preceding IF IF X < Y DISPLAY “XXX” IF Y < Z DISPLAY “ZZZ” ELSE DISPLAY “AAA” END-IF• MORAL: Indent properly and terminate all if statements

with END-IF

Page 136: Enterprise COBOL Concepts

EVALUATE

Page 137: Enterprise COBOL Concepts

EVALUATE

EVALUATE PLANET-NUMBER WHEN 1 MOVE "Mercury" TO PLANET-NAME WHEN 2 MOVE "Venus " TO PLANET-NAME WHEN 3 MOVE "Earth " TO PLANET-NAME WHEN 4 MOVE "Mars " TO PLANET-NAME WHEN 5 MOVE "Jupiter" TO PLANET-NAME WHEN 6 MOVE "Saturn " TO PLANET-NAME WHEN 7 MOVE "Uranus " TO PLANET-NAME WHEN 8 MOVE "Neptune" TO PLANET-NAME WHEN 9 MOVE "Pluto " TO PLANET-NAME WHEN OTHER MOVE " " TO PLANET-NAMEEND-EVALUATE.

Page 138: Enterprise COBOL Concepts

EVALUATE

EVALUATE PLANET-NAME WHEN "Mercury" MOVE 1 TO PLANET-NUMBER WHEN "Venus " MOVE 2 TO PLANET-NUMBER WHEN "Earth " MOVE 3 TO PLANET-NUMBER WHEN "Mars " MOVE 4 TO PLANET-NUMBER WHEN "Jupiter" MOVE 5 TO PLANET-NUMBER WHEN "Saturn " MOVE 6 TO PLANET-NUMBER WHEN "Uranus " MOVE 7 TO PLANET-NUMBER WHEN "Neptune" MOVE 8 TO PLANET-NUMBER WHEN "Pluto " MOVE 9 TO PLANET-NUMBER WHEN OTHER MOVE 0 TO PLANET-NUMBER END-EVALUATE.

Page 139: Enterprise COBOL Concepts

EVALUATEEVALUATE TRUE WHEN PLANET-NAME = "Mercury" MOVE 1 TO PLANET-NUMBER WHEN PLANET-NAME = "Venus " MOVE 2 TO PLANET-NUMBER WHEN PLANET-NAME = "Earth " MOVE 3 TO PLANET-NUMBER WHEN PLANET-NAME = "Mars " MOVE 4 TO PLANET-NUMBER WHEN PLANET-NAME = "Jupiter" MOVE 5 TO PLANET-NUMBER WHEN PLANET-NAME = "Saturn " MOVE 6 TO PLANET-NUMBER WHEN PLANET-NAME = "Uranus " MOVE 7 TO PLANET-NUMBER WHEN PLANET-NAME = "Neptune" MOVE 8 TO PLANET-NUMBER WHEN PLANET-NAME = "Pluto " MOVE 9 TO PLANET-NUMBER WHEN OTHER MOVE 0 TO PLANET-NUMBER END-EVALUATE.

Page 140: Enterprise COBOL Concepts

EVALUATEEVALUATE Qty ALSO TRUE ALSO Member WHEN 1 THRU 5 ALSO VOP < 501 ALSO "Y" MOVE 2 TO Discount WHEN 6 THRU 16 ALSO VOP < 501 ALSO "Y" MOVE 3 TO Discount WHEN 17 THRU 99 ALSO VOP < 501 ALSO "Y" MOVE 5 TO Discount WHEN 1 THRU 5 ALSO VOP < 2001 ALSO "Y" MOVE 7 TO Discount WHEN 6 THRU 16 ALSO VOP < 2001 ALSO "Y" MOVE 12 TO Discount WHEN 17 THRU 99 ALSO VOP < 2001 ALSO "Y" MOVE 18 TO Discount WHEN 1 THRU 5 ALSO VOP > 2000 ALSO "Y" MOVE 10 TO Discount WHEN 6 THRU 16 ALSO VOP > 2000 ALSO "Y" MOVE 23 TO Discount END-EVALUATE

Page 141: Enterprise COBOL Concepts

EVALUATEEVALUATE TRUE ALSO Position WHEN L-Arrow ALSO 2 THRU 10 SUBTRACT 1 FROM Position WHEN R-Arrow ALSO 1 THRU 9 ADD 1 TO Position WHEN L-Arrow ALSO 1 MOVE 10 TO Position WHEN R-Arrow ALSO 10 MOVE 1 TO Position WHEN DelKey ALSO ANY PERFORM DeleteChar WHEN Char ALSO 1 THRU 9 PERFORM InsertChar ADD 1 TO Position WHEN Char ALSO 10 PERFORM InsertChar WHEN OTHER PERFORM DisplayErrorMessage END-EVALUATE

Page 142: Enterprise COBOL Concepts

PERFORM

Page 143: Enterprise COBOL Concepts

PERFORM Paragraph

• PERFORM paragraph name– Execute all instructions in the paragraph– Return control to the next instruction after the PERFORM

PERFORM 100-ROUTINEPERFORM 200-ROUTINEPERFORM 100-ROUTINE…100-ROUTINE. …200-ROUTINE. …300-ROUTINE.

Page 144: Enterprise COBOL Concepts

PERFORM THRU• PERFORM paragraph name THRU paragraph name

PERFORM 100-XXX THUR 100-XXX-EXIT 100-XXX. DISPLAY ‘IN 100-XXX’. 100-XXX-EXIT. EXIT.• There is an implicit EXIT in every paragraph so why do I need to code it explicitly?

Page 145: Enterprise COBOL Concepts

Perform Thru (Let it Die)

• I view use of PERFORM…THRU as an older, unnecessary style, caused by a compiler problem that existed many years ago. Using PERFORM THRU was a way to address this ancient problem. The problem has been fixed for many years now, yet old habits die hard.

Page 146: Enterprise COBOL Concepts

PERFORM WITH TIMES

Page 147: Enterprise COBOL Concepts

PERFORM x TIMES

MOVE 5 TO COUNT

PERFORM COUNT TIMES

DISPLAY “XXX”

END-PERFORM

PERFORM 100-DISPLAY COUNT TIMES

Page 148: Enterprise COBOL Concepts

PERFORM UNTIL

Page 149: Enterprise COBOL Concepts

PERFORM UNTIL

• MOVE 0 TO X PERFORM UNTIL X > 10 MOVE X TO X-EDITED DISPLAY X-EDITED ADD 1 TO X END-PERFORM• PERFORM X-PARA UNTIL X > 10• PERFORM X-PARA WITH TEST AFTER UNTIL X > 10

Page 150: Enterprise COBOL Concepts

PERFORM VARYING

Page 151: Enterprise COBOL Concepts

Inline Perform

PERFORM VARYING X FROM 1 BY 1 UNTIL X > 100 DISPLAY XEND-PERFORM

PRINTS:123…100

Page 152: Enterprise COBOL Concepts

Inline PERFORM

PERFORM VARYING X FROM 5 BY -1 UNTIL X =0 DISPLAY XEND-PERFORM

PRINTS: 543210

Page 153: Enterprise COBOL Concepts

Inline PERFORM

MOVE 10 TO X

PERFORM WITH TEST AFTER

UNTIL X = 0

DISPLAY X

SUBTRACT 1 FROM X

END-PERFORM

Page 154: Enterprise COBOL Concepts

PERFORM PARAGRAPH

PERFORM 100-RTN

WITH TEST AFTER

VARYING X FROM 1 BY 1

UNTIL X = 100

100-RTN.

….

Page 155: Enterprise COBOL Concepts

Inline PERFORM

MOVE ZERO TO Y

PERFORM UNTIL X = 0

READ… AT END MOVE 0 TO X

ADD X TO Y

DISPLAY Y

END-PERFORM

Page 156: Enterprise COBOL Concepts

Alternate PERFORM PERFORM 100-PARA VARYING I FROM 1 BY 1 UNTIL I > 5 AFTER J FROM 1 BY 1 UNTIL J > 3 END-PERFORM100-PARA. DISPLAY I J .1 11 21 32 12 22 33 13 23 34 1 …

Page 157: Enterprise COBOL Concepts

Table Processing with Subscripts

01 TOT PIC S9(8) PACKED DECIMAL.01 SUB1 PIC 99.01 TEMP-REC. 05 TEMP OCCURS 12 TIMES PIC S9(4).…MOVE 0 TO TOTPERFORM VARYING SUB1 FROM 1 BY 1 UNTIL SUB1 > 12 ADD TEMP(SUB1) TO TOT ENDPERFORM

Page 158: Enterprise COBOL Concepts

Table Processing with Indexes

01 TOT PIC S9(8) PACKED DECIMAL.

01 TEMP-REC. 05 TEMP OCCURS 12 TIMES INDEXED BY K PIC S9(4).…MOVE 0 TO TOTPERFORM VARYING K FROM 1 BY 1 UNTIL K > 12 ADD TEMP(K) TO TOT END-PERFORM

Page 159: Enterprise COBOL Concepts

Manipulating Indexes

• Indexes can’t be manipulated with ordinary arithmetic commands. Instead use SET.

• SET K TO 3• SET K UP BY 1• SET K UP BY 2• SET K DOWN BY 3

Page 160: Enterprise COBOL Concepts

CONTINUE

Used to indicate that no operation is present.

Page 161: Enterprise COBOL Concepts

EXIT

• Sometimes used to provide an explicit exit from a paragraph or a program.

• There is an implicit exit at the end of each paragraph• 100-PARA. DISPLAY X EXIT .

Page 162: Enterprise COBOL Concepts

STOP

• Usually used to permanently halt execution of a program

• Can be used to temporarily halt execution and require operator intervention

Page 163: Enterprise COBOL Concepts

GOBACK

• Functions like an EXIT PROGRAM when coded at the end of a called program

• Functions like STOP RUN when coded in a main program

• I prefer coding this in place of STOP RUN

Page 164: Enterprise COBOL Concepts

GO TO

• Causes an unconditional jump in program execution to the procedure that is named.

• This statement should be used only in very special situations, for instance, to branch to an error routine that terminates the program from a deeply nested area of your program.

• Overuse (any?) of this statement is unnecessary and leads to spaghetti code

• Don’t even think of using the alternate forms of GO TO !

Page 165: Enterprise COBOL Concepts

Exercise #2

• Create a file of 80 byte records• Each record has 3 fields• CUSTLNAME – CHARACTER 15• CUSTFNAME - CHARACTER 15• BALANCE - PACKED DECIMAL – 5 BYTES 2 DECS• Create a table of the file data using subscripts.• Sort the table with a bubble sort on customer name.• Print the sorted table: Last Name First Name Balance

Page 166: Enterprise COBOL Concepts

Exercise #3

• Repeat Exercise #2 using indexes

Page 167: Enterprise COBOL Concepts

SEQUENTIAL SEARCH

Page 168: Enterprise COBOL Concepts

SEARCH• Search performs a sequential search with an index• Rule of thumb: Use SEARCH for tables with 20 items or less• DEPENDING ON field must contain the number of table entries01 RECCOUNT PIC 9(2).01 SALES-TAX. 05 TAB-ENTRIES OCCURS 20 TIMES DEPENDING ON RECCOUNT INDEXED BY K. 10 ZIPCODE PIC 9(5). 10 RATE PIC V999.

SET K TO 1SEARCH TAB-ENTRIES AT END MOVE 0 TO TAX WHEN ZIPIN = ZIPCODE(K) COMPUTE TAX = RATE(K) * AMOUNTEND-SEARCH

Page 169: Enterprise COBOL Concepts

Exercise #4

• Read the file from exercise #3

• Store the data in a table

• Read the file BCST.SICCC01.PDSLIB(DAT4EXER). Each record has a LAST NAME, FIRST NAME field:

Last name – columns 1 – 15 First name – columns 16 – 30• Code a Sequential Search statement to find each name

in the file. Print each name, “Found” or “Not Found”, and the customer balance if found.

Page 170: Enterprise COBOL Concepts

SEARCH ALL

Page 171: Enterprise COBOL Concepts

SEARCH ALL• SEARCH ALL performs a binary search with an index• ENTRIES MUST BE IN ORDER BY KEY! • No SET necessary (the whole table is searched)• RECCOUNT must contain the number of table entries.01 RECCOUNT PIC 999 PACKED-DECIMAL.01 SALES-TAX. 05 TAB-ENTRIES OCCURS 100 TIMES DEPENDING ON RECCOUNT ASCENDING KEY ZIPCODE INDEXED BY K. 10 ZIPCODE PIC 9(5). 10 RATE PIC V999.

SEARCH ALL TAB-ENTRIES AT END MOVE 0 TO TAX WHEN ZIPCODE(K) = ZIPIN COMPUTE TAX = RATE(K) * AMOUNTEND-SEARCH

Page 172: Enterprise COBOL Concepts

SEARCH ALL CONSTRAINTS

• The condition following WHEN must test for equality• Compound conditions with ANDs not ORs• Only one WHEN clause• VARYING not allowed• OCCURS item and its index must appear on the left of the

equal sign– WHEN TEMP(K) = 80

Page 173: Enterprise COBOL Concepts

SEARCH ALL Constraints

• Table must indicate ASCENDING or DESCENDING KEY

01 TABLE. 05 CUST-REC OCCURS 40 TIMES DEPENDING ON RECCOUNT ASCENDING KEY CUST INDEXED BY K. 10 CUST PIC 9(4). 10 RATE PIC V999.

Page 174: Enterprise COBOL Concepts

Exercise #5

• Read the file from exercise #3

• Store the data in a table

• Read the file BCST.SICCC01.PDSLIB(DAT4EXER). Each record has a LAST NAME , FIRST NAME fields

Last name – columns 1 – 15 First name – columns 16 – 30• Code a binary search statement to find each name in the

file. Print each name, “Found” or “Not Found”, and the customer balance if found.

Page 175: Enterprise COBOL Concepts

STRING

Page 176: Enterprise COBOL Concepts

STRING

• Used to build string expressions by concatenation (blanks not stored)

STRING FNAME DELIMITED BY “ “

MNAME DELIMITED BY “ “

LNAME DELIMITED BY “ “

INTO NAME-OUT

Page 177: Enterprise COBOL Concepts

STRING

• Blanks stored• DELIMITED BY SIZE means include the entire literal

or variable contents

STRING FNAME DELIMITED BY “ “ “ “ DELIMITED BY SIZE MNAME DELIMITED BY “ “ “ “ DELIMITED BY SIZE LNAME DELIMITED BY “ “ “ “ DELIMITED BY SIZE INTO NAME-OUT

Page 178: Enterprise COBOL Concepts

STRING

Page 179: Enterprise COBOL Concepts

UNSTRING

Page 180: Enterprise COBOL Concepts

UNSTRING

• Separates a string into several component strings

• Sending field must be alphanumeric

UNSTRING NAME DELIMITED BY “,” INTO LNAME FNAME MIEND-UNSTRING

Page 181: Enterprise COBOL Concepts

UNSTRING

UNSTRING JCL-DATA

DELIMITED BY ALL SPACES OR ALL ','

INTO WS-DATE-REQUESTED

WS-DATE1

WS-DATE2

END-UNSTRING

Page 182: Enterprise COBOL Concepts

Exercise #6

• Read the file

BCST.SICCC01.PDSLIB(STRINGS)• Print the first name, middle initial, and last

names in columns

Page 183: Enterprise COBOL Concepts

Exercise #7

• Read the file BCST.SICCC01.PDSLIB(STRINGS1)

• Print the digits followed by the letters. Use “/” as the delimiter of the two fields.

Page 184: Enterprise COBOL Concepts

CALL

Page 185: Enterprise COBOL Concepts

Static and Dynamic Call

• Programs A and B that are linked together prior to being loaded are statically linked. If A calls B, the call is static

• If programs A and B are separately compiled and linked, A can call B dynamically:

01 PGMNAME PIC X(8) VALUE ‘B’. … CALL PGMNAME

Page 186: Enterprise COBOL Concepts

Calling Other Programs Statically

CALL literal program name

USING identifier-1, …

Examples

CALL ‘CUST1030’ USING X,Y

CALL ‘PROG1000’

Page 187: Enterprise COBOL Concepts

CALLING ANOTHER PROGRAM

CALL ‘PROGXXXX’ USING A,B

IDENTIFICATION DIVISION.PROGRAM-ID. PROGXXXX.…LINKAGE SECTION.01 X PIC X(5).01 Y PIC 999V99.PROCEDURE DIVISION USING X,Y.… GOBACK .

Page 188: Enterprise COBOL Concepts

Static and Dynamic CALLS

• Two methods for generating dynamic calls

1. CALL MYPROG USING X,Y,Z (Using an identifier insures a dynamic

call.)2. DYNAM/NODYNAM compiler option

determines whether a static or dynamic call occurs

3. At TSYS all calls are dynamic.

Page 189: Enterprise COBOL Concepts

Exercise #8

• Write a main program that,

1) Prints “I am in the main program”,

2) Calls your subprogram,

3) Prints “I am back in the main program”

• Write a subprogram that prints “I am in the subprogram.

• Compile and link the programs, execute the main program

Page 190: Enterprise COBOL Concepts

Exercise #9

• Write a main program that passes 3 packed decimal numbers to a subprogram and a fourth variable in which to receive a result.

• Write a subprogram that accepts the 3 integers, computes their sum and returns the answer to the main

• Have the main print the result

Page 191: Enterprise COBOL Concepts

Exercise #10

• Write a main program that passes a variable X by reference and Y by content

• Have the subprogram add one to both numbers

• Have the main program print X and Y after returning from the subprogram

Page 192: Enterprise COBOL Concepts

NUMERIC Class Test

• Before using a “suspect” field that has a PIC of 9’s, use the NUMERIC class test to verify the field before doing arithmetic

IF AMOUNT-IN IS NUMERIC

ADD 1 TO AMOUNT-IN

ELSE

DISPLAY “AMOUNT IS NOT NUMERIC”

END-IF

Page 193: Enterprise COBOL Concepts

Sign Test

• Numeric data can be tested for positive, negative, and zero values

IF AMOUNT-IN IS POSITIVE ADD 1 TO AMOUNT-INEND-IFIF AMOUNT-IN IS NEGATIVE DISPLAY AMOUNT-INEND-IFIF AMOUNT-IN IS ZERO DISPLAY “THE FIELD IS ZERO”END-IF

Page 194: Enterprise COBOL Concepts

INSPECT (TALLYING)

Page 195: Enterprise COBOL Concepts

INSPECT (TALLYING)INSPECT MYLINE TALLYING ECOUNT FOR ALL “E“ AFTER INITIAL “START" BEFORE INITIAL “END“END-INSPECT INSPECT WORK TALLYING COUNT1 FOR LEADING “*” COUNT2 FOR CHARACTERSEND-INSPECTINSPECT WORK TALLYING COUNT1 FOR ALL “*” BEFORE “.” COUNT2 FOR ALL CHARACTERS AFTER “.”END-INSPECT

Page 196: Enterprise COBOL Concepts

INSPECT (REPLACING)

Page 197: Enterprise COBOL Concepts

INSPECT (REPLACING)INSPECT MYDATA REPLACING ALL “X" BY “Y“ AFTER INITIAL “A" BEFORE INITIAL “Z“

INSPECT MYDATA REPLACING LEADING “ " BY “+“

INSPECT MYDATA REPLACING ALL “A" BY “+“ AFTER INITIAL “X" INSPECT MYDATA REPLACING FIRST “A" BY “+" AFTER INITIAL “A" BEFORE INITIAL “Z“

INSPECT MYDATA REPLACING ALL “AAAA" BY “ZZZZ"

Page 198: Enterprise COBOL Concepts

INSPECT (CONVERTING)

Page 199: Enterprise COBOL Concepts

INSPECT (CONVERTING)

INSPECT TEXTLINE CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

INSPECT FIELDA CONVERTING

“1234567890” TO “ABCDEFGHIJ”

Page 200: Enterprise COBOL Concepts

REFERENCE MODIFICATION

• Allows you to process PIC 9 and PIC X fields as if they were an array of characters.

• FIELDNAME(start pos : [ length])

05 FIELDA PIC X(7) VALUE ‘ABCDEFG’.

FIELDA(1:3) “ABC”FIELDA(2:2) “BC”FIELDA(4: ) “DEFG”

Page 201: Enterprise COBOL Concepts

Qualification of Names

• COBOL allows the same variable name to be used to define fields in different records or group items.

• Duplicate names must be qualified when they are referenced

01 XFIELD. 05 YFIELD. 10 ZFIELD PIC X(3).If ZFIELD is a duplicate name it can be qualified in two

ways: ZFIELD OF YFIELD ZFIELD OF XFIELD

Page 202: Enterprise COBOL Concepts

Intrinsic Functions

• COBOL does not permit user-defined functions or procedures

• Intrinsic (built-in) Functions can be used in your programs

• Three broad categories of intrinsic functions: date functions, numeric functions and string functions.

Page 203: Enterprise COBOL Concepts

Intrinsic Functions

• Intrinsic Function values are replaced in the position where they occur by the function result.

• In COBOL, an Intrinsic Function is a temporary data item whose value is determined at the time the function is executed.

• Functions that return a number value (numeric & integer) are always considered to be signed.

• A function that returns a number value can be used only in an arithmetic expression or as the source of a MOVE statement.

Page 204: Enterprise COBOL Concepts

Intrinsic Functions

• Intrinsic function pattern:

FUNCTION FunctionName(Parameters)• FunctionName is the name of the function and

Parameters is one or more parameters supplied to the function.

COMPUTE NUM = FUNCTION RANDOM(99)

MOVE FUNCTION REVERSE(“ABCD”) TO NAME

Page 205: Enterprise COBOL Concepts

Sample Intrinsic Functions

• CHAR(PosInt) AlphanumericReturns the character at ordinal position PosInt of the collating sequence.

• ORD(Alph) IntegerReturns the ordinal position of character Alph.

• ORD-MAX({Any}...)IntegerReturns the ordinal position of whichever of the parameters has the highest value. All parameters must be of the same type. The parameter list may be replaced by an array.

• ORD-MIN({Any}...) IntegerReturns the ordinal position of whichever of the parameters has the lowest value. All parameters must be of the same type.

Page 206: Enterprise COBOL Concepts

Intrinsic Functions

• REVERSE(Alph) Alphanumeric Returns a character string with the characters in Alph reversed.

• LOWER-CASE(Alph) Alphanumeric Returns a character string with the characters in Alph changed to their lower case equivalents.

• UPPER-CASE(Alph) Alphanumeric Returns a character string with the characters in Alph changed to their upper case equivalents

Page 207: Enterprise COBOL Concepts

Date Intrinsic Functions

• CURRENT-DATE - Returns a 21 character string representing the current date and time

• DATE-OF-INTEGER(PosInt) - Returns the yyyymmdd (standard date) equivalent of the integer date - PosInt. The integer date is the number of days that have passed since Dec 31st 1600 in the Gregorian Calendar.

Page 208: Enterprise COBOL Concepts

Math Intrinsic Functions

• MAX({Any}...)Return type on type of Any. Takes a parameter list and returns the content of

whichever parameter contains the maximum value.

• The returned type depends upon the parameter types as follows; Alphanumeric if parameters are Alphabetic or Alphnumeric.Integer if all are integer.Numeric if all are Numeric. An array may be used instead of the parameter list.

Page 209: Enterprise COBOL Concepts

Math Intrinsic Functions

• SQRT(Num) Returns an approximation of the square root of Num.

• STANDARD-DEVIATION({Num}...)

Returns an approximation of the standard deviation of its parameters.

• And many others…

Page 210: Enterprise COBOL Concepts

Condition Names

01 NO-OF-NEIGHBORS PIC 9.

88 JUST-RIGHT VALUE 2 THRU 3.

88 TOO-FEW VALUE 0 THRU 1.

88 TOO-MANY VALUE 4 THRU 8.

01 MARITAL-STATUS PIC X.

88 VALID-STATUS

VALUE ‘S’ ‘M’ ‘D’ ‘W’.

Page 211: Enterprise COBOL Concepts

Variable Length Tables

01 TCOUNT PIC S9(3) PACKED-DECIMAL.01 STATE-TABLE. 05 ST-GROUP OCCURS 1 TO 100 TIMES DEPENDING ON TCOUNT ASCENDING KEY IS ZIP INDEXED BY NDX. 10 ZIP PIC X(5). 10 RATE PIC V9999.

Page 212: Enterprise COBOL Concepts

Loading a Variable Length Table

PERFORM WITH TEST AFTER VARYING NDX FROM 1 BY 1 UNTIL PTABLE-EOF OR NDX = 100 PERFORM FILE-READ IF NOT PTABLE-EOF MOVE ZIP-IN TO ZIP(NDX) MOVE RATE-IN TO RATE(NDX) ELSE SET NDX DOWN BY 1 SET TCOUNT TO NDX END-IFEND-PERFORM

Page 213: Enterprise COBOL Concepts

Exercise #11• Read the file BCST.SICCC01.PDSLIB(STATES)

• Store the data in a variable length table

• Read the file BCST.SICCC01.PDSLIB(COBDATA4). Each record has a 2 digit state code and a 5 byte zip code. For each record in this file print a record on a report. The report will have the following format:

GA 31907 GEORGIA - VALID ZIP AND STATE CA 90003 CALIFORNIA - INVALID ZIP ZD 30002 INVALID STATE

• Capitalize the state name found in the table. Print message next to each state name separated by a dash.

• Use the SEARCH command perform a sequential search of the table for each look up. After the program is working, modify it to perform a binary search with SEARCH

ALL

Page 214: Enterprise COBOL Concepts

Files with Multiple Record Types

FD TransFile. 01 InsertRec. 02 RECI PIC X. 02 STUDENTIDI PIC 9(7). 02 STUDENTNAME. 03 SURNAME PIC X(8). 03 INITIALS PIC XX. 02 DOB. 03 YOBIRTH PIC 9(4). 03 MOBIRTH PIC 99. 03 DOBIRTH PIC 99. 02 COURSECODE PIC X(4). 02 GENDER PIC X. 01 DELETEREC. 02 RECD PIC X. 02 STUDENTIDD PIC 9(7). 01 UpdateRec. 02 STUDENTIDU PIC 9(7). 02 NEWCOURSECODE PIC X(4).

Page 215: Enterprise COBOL Concepts

Multiple O1 File Descriptions

• Any number of 01 record descriptions can be coded with the FD

• Only one buffer is used no matter how many record descriptions have been coded

• Record fields can’t be referenced before the file is opened or after it is closed

• With multiple record formats, there needs to be a fixed field to indicate the record type

• Value clauses are only used for 88 level items

Page 216: Enterprise COBOL Concepts

Writing With Carriage Control

Page 217: Enterprise COBOL Concepts

Variable Length Records

• The RECORD IS VARYING IN SIZE clause specifies a file containing variable length records.

Page 218: Enterprise COBOL Concepts

Variable Length Records

• The RECSIZE number in the DEPENDING ON phase must be an elementary unsigned integer data-item declared in the WORKING-STORAGE SECTION.

FD TRANFILE RECORD IS VARYING IN SIZE FROM 1 TO 80 CHARACTERS DEPENDING ON RECSIZE.

Page 219: Enterprise COBOL Concepts

Variable Length Record Processing

• When writing a variable length record, the size of the record must be placed in the RECSIZE variable before the WRITE is issued.

• When reading a variable length record, the length of the record is delivered into the RECSIZE variable.

• The 01 Record description must be long enough to accommodate the largest record

Page 220: Enterprise COBOL Concepts

Variable length Record Processing

FD TRANFILE RECORD IS VARYING IN SIZE FROM 1 TO 80 CHARACTERS DEPENDING ON RECSIZE. 01 TRANREC PIC X(80). 88 END-OF-RECS VALUE HIGH-VALUES.

WORKING-STORAGE SECTION. 01 RECSIZE PIC 99.

READ TRANFILE AT END … DISPLAY TRANREC(1:RECSIZE)

Page 221: Enterprise COBOL Concepts

Writing Variable Length Records

• Many variable length records have a fixed front end, and varying numbers of segments

FD CUST RECORD IS VARYING IN SIZE FROM 28 TO 408 CHARACTERS DEPENDING ON RECORD-LEN.01 CUST-REC. 05 ROOT-SEG. 10 CUST-NO PIC X(6). 10 INVOICE-COUNT PIC S99. 05 INVOICE-SEGMENT OCCURS 20 TIMES INDEXED BY NDX 10 INVOICE-DATE PIC X(8). 10 INVOICE-NO PIC X(5). 10 INVOICE-AMT pIC S9(5)V99.• You must set the RECORD-LEN before writing the record!

Page 222: Enterprise COBOL Concepts

Alternative

FD CUSTFILERECORD CONTAINS 20 TO 80 CHARACTERS01 REC. 05 FIXED-PART PIC X(20). 10 … 05 VARY-PART OCCURS 1 TO 6 TIMES DEPENDING ON COUNT INDEXED BY NDXCOUNT has to be initialized at the time of writing the record

Page 223: Enterprise COBOL Concepts

Reading Variable Length Records

FD CUST RECORD IS VARYING IN SIZE FROM 28 TO 408 CHARACTERS. 01 CUST-REC. 05 ROOT-SEG. 10 CUST-NO PIC X(6). 10 INVOICE-COUNT PIC S99. 05 INVOICE-SEGMENT OCCURS 20 TIMES INDEXED BY NDX 10 INVOICE-DATE PIC X(8). 10 INVOICE-NO PIC X(5). 10 INVOICE-AMT PIC S9(5)V99.… PERFORM VARYING NDX FROM 1 BY 1 UNTIL NDX > INVOICE-COUNT ADD INVOICE-AMT(NDX) TO AMT-OWED END-PERFORM

Page 224: Enterprise COBOL Concepts

EXERCISE #12

• Each A record for a given customer is followed by one to five B records for that customer.

• For each A record, write out one variable length record that contains the A record as the fixed part and the associated B records as the variable parts

Page 225: Enterprise COBOL Concepts

EXERCISE #13

BCST.SICCC01.PDSLIB(COBDATA5) CONTAINS TWO TYPES OF 80 BYTE RECORDS :

RECORD TYPE “A”1 BYTE TYPE CODE PIC X CONTAINING “A”5 BYTE CUSTOMER ID PIC X(5)

RECORD TYPE “B”1 BYTE TYPE CODE PIC X CONTAINING “B”5 BYTE PART NUMBER PIC X(5)6 BYTE COST PIC 9(4)V99

Page 226: Enterprise COBOL Concepts

EXERCISE #14

Read the variable length records you created in exercise #5. Produce a report similar to the one below:

CUSTOMER ID PART # COST 10030 22322 1,333.34 23444 3.44 50043 98.77TOTAL 1435.55 20030 22322 1,333.34 23444 3.44 50043 98.77TOTAL 1435.55 … …

Page 227: Enterprise COBOL Concepts

VSAM File Processing

Virtual Storage Access Method

Page 228: Enterprise COBOL Concepts

VSAM File Types

• ESDS – Entry Sequenced Data Set– Allows sequential processing

• RRDS – Relative Record Data Set– Allows sequential or random access by

relative record number

• KSDS – Key-Sequenced Data Set– Allows sequential, skip sequential, and

random processing by key

Page 229: Enterprise COBOL Concepts

VSAM

• VSAM data sets are known as Clusters

• For ESDS or RRDS the cluster consists of a data component

• For KSDS the cluster consists of a data component and an index component

• VSAM data is stored on DASD in control intervals which are grouped into control areas

Page 230: Enterprise COBOL Concepts

VSAM

• The Control Interval (CI) is the unit of data that transfers between the disk and virtual storage

• CI sizes are multiples of 2K with 4k being common

• CI’s can be constructed with free space to accommodate additions to the file

• Control Areas (CA) can be constructed with free space to accommodate additions

Page 231: Enterprise COBOL Concepts

VSAM

• VSAM dynamically manages the file by maintaining information in each CI and CA

• When a CI becomes too “full” the data it contains is split into two CI’s

• When a CA becomes too “full” the data it contains is split into two CA’s

• VSAM tries to keep records that are logically close together, physically close as well

Page 232: Enterprise COBOL Concepts

VSAM Indexes

Page 233: Enterprise COBOL Concepts

VSAM Components

Page 234: Enterprise COBOL Concepts

Access Method Services (AMS)

• AMS is a VSAM utility that provides numerous options– DEFINE CLUSTER– PRINT– REPRO– LISTCAT– DELETE– DEFINE ALTERNATEINDEX– DEFINE PATH– BLDINDEX

Page 235: Enterprise COBOL Concepts

VSAM JCL

• Unlike QSAM files, VSAM files are usually allocated in a separate job step before data can be written to the file

• A VSAM cluster is usually created by deleting and then defining the cluster

• After the cluster is defined, a job can run which writes data to the file

Page 236: Enterprise COBOL Concepts

VSAM JCL

• Parameters:– INDEXED –KSDS– NONINDEXED – ESDS– NUMBERED – RRDS– KEYS ( len off) – primary key info– CISZ (size) – control interval size– FREESPACE (ci ca) – free space %’s

Page 237: Enterprise COBOL Concepts

MAKEKSDS• 000100 //TSYSAD2C JOB 'YOUR NAME',USER=TSYSAD2,REGION=2048K,MSGCLASS=V• 000200 //*MAIN CLASS=TSYSC,USER=TSYSAD2• 000300 //DEFINE EXEC PGM=IDCAMS• 000400 //SYSPRINT DD SYSOUT=*• 000500 //SYSIN DD *• 000600 DELETE TSYSAD2.PAYROLL.MASTER• 000700 DEFINE CLUSTER -• 000800 (NAME(TSYSAD2.PAYROLL.MASTER) -• 000900 INDEXED -• 001000 RECORDSIZE(31 31) -• 001100 KEYS(5 0) -• 001200 MGMTCLAS(STANDARD) -• 001210 FREESPACE(0 0) -• 001220 SHAREOPTIONS (3 3)) -• 001230 DATA (NAME(TSYSAD2.PAYROLL.MASTER.DATA) -• 001240 TRK(1 1) -• 001250 CONTROLINTERVALSIZE(4096)) -• 001260 INDEX (NAME(TSYSAD2.PAYROLL.MASTER.INDEX) -• 001270 TRK(1 1))• 001280 /*

Page 238: Enterprise COBOL Concepts

IDCAMS PRINT000100 //TSYSAD2P JOB

'A.STUDENT',USER=TSYSAD2,REGION=2048K,MSGCLASS=V 000200 //*MAIN CLASS=TSYSC,USER=TSYSAD2 000210 //* THIS IS AN IDCAMS PRINT 000220 //PRINT EXEC PGM=IDCAMS 000230 //SYSPRINT DD SYSOUT=* 000240 //SYSIN DD * 000250 PRINT INFILE(IFILE) - 000251 DUMP 000252 /* 000253 //IFILE DD DSN=TSYSAD2.PAYROLL.MASTER,DISP=SHR 000254 //

Page 239: Enterprise COBOL Concepts

IDCAMS REPRO• 000100 //TSYSAD2R JOB 'A.STUDENT',USER=TSYSAD2,REGION=2048K,MSGCLASS=V• 000200 //*MAIN CLASS=TSYSC,USER=TSYSAD2• 000210 //* THIS AN IDCAMS REPRO• 000220 //REPRO EXEC PGM=IDCAMS• 000230 //FILEIN DD DSN=TSYSAD2.PGM1.RESULTS,DISP=SHR• 000240 //FILEOUT DD DSN=TSYSAD2.I10.PGM1.RESULTS,DISP=(NEW,CATLG,DELETE),• 000250 // UNIT=SYSDA,DCB=(RECFM=FB,LRECL=80),• 000251 // SPACE=(TRK,(1,1),RLSE)• 000252 //SYSIN DD *• 000253 REPRO -• 000254 INFILE(FILEIN) -• 000255 OUTFILE(FILEOUT)• 000256 /*• 000257 //AMSDUMP DD SYSOUT=*• 000258 //

Page 240: Enterprise COBOL Concepts

Creating a VSAM File• 000100 IDENTIFICATION DIVISION.• 000200 PROGRAM-ID. VSAM1.• 000300 ENVIRONMENT DIVISION.• 000400 INPUT-OUTPUT SECTION.• 000500 FILE-CONTROL.• 000600 SELECT PAYROLL-MASTER-OUT ASSIGN TO PAYMASTO• 000610 ORGANIZATION IS INDEXED• 000620 ACCESS IS SEQUENTIAL• 000630 RECORD KEY IS ID-OUT• 000640 FILE STATUS IS PM-STATUS.• 000700 SELECT PAYROLL-MASTER-IN ASSIGN TO PAYMASTI.

Page 241: Enterprise COBOL Concepts

Creating a VSAM File• 004410 01 PM-STATUS.• 004430 05 PM-STAT1 PIC X.• 004440 05 PM-STAT2 PIC X.• 004441 PROCEDURE DIVISION.• 004450 OPEN INPUT PAYROLL-MASTER-IN• 004460 OPEN OUTPUT PAYROLL-MASTER-OUT• 004461 IF PM-STATUS NOT = '00'• 004462 PERFORM 300-PRINT-STATUS• 004463 END-IF• 004470 PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '• 004480 READ PAYROLL-MASTER-IN• 004490 AT END• 004500 MOVE 'NO ' TO ARE-THERE-MORE-RECORDS• 004600 NOT AT END• 004700 PERFORM 200-READ-MODULE• 004800 END-READ• 004900 END-PERFORM• 005000 CLOSE PAYROLL-MASTER-IN• 005100 PAYROLL-MASTER-OUT• 005110 GOBACK

Page 242: Enterprise COBOL Concepts

Creating a VSAM File• 005130 200-READ-MODULE.• 005410 MOVE ID-IN TO ID-OUT• 005420 MOVE NAME-IN TO NAME-OUT• 005430 MOVE HOURS-IN TO HOURS-OUT• 005440 MOVE RATE-IN TO RATE-OUT• 005500 WRITE MASTER-REC-OUT• 005510 IF PM-STATUS NOT = '00'• 005520 PERFORM 300-PRINT-STATUS• 005530 END-IF• 005600 .• 005700 300-PRINT-STATUS.• 005800 DISPLAY 'FILE STATUS CODE:' PM-STATUS• 005900 GOBACK• 006000 .

Page 243: Enterprise COBOL Concepts

VSAM Error Strategy

• VSAM returns a status code after each operation

• It is imperative that you check each status code after each operation to insure that the program is proceeding normally

• The status code is a two byte field

Page 244: Enterprise COBOL Concepts

OPEN

• OPEN INPUT file-name …• OPEN OUTPUT file-name …• OPEN I-O file-name …• OPEN EXTEND file-name …

For EXTEND, access mode must be sequential

Page 245: Enterprise COBOL Concepts

Reading for Sequential Access

READ file-name [NEXT] [RECORD] [INTO data-name] [AT END imperative stmt] [NOT AT END imperative stmt][END-READ]Specify NEXT if access is DYNAMIC and you want

sequential processingCan be omitted when access is SEQUENTIALINTO provides move mode I/OOmitting INTO provides locate mode I/O

Page 246: Enterprise COBOL Concepts

Reading for Random Access

READ file-name [RECORD]

[INTO data-name]

[INVALID KEY imperative stmt]

[NOT INVALID KEY imperative stmt]

[END-READ]

Be sure to set the key of the record you wish to read beforehand

Page 247: Enterprise COBOL Concepts

Writing

WRITE record-name [FROM data-name]

[INVALID KEY imperative stmt]

[NOT INVALID KEY imperative stmt]

[END-WRITE]

Page 248: Enterprise COBOL Concepts

KSDS FILE CREATION

SELECT INVMAST ASSIGN TO INVMAST ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS ITEM-NO FILE STATUS IS FILE-STAT.FD INVMAST01 RECORD-AREA. 05 ITEM-NO PIC X(5) 05 PIC X(75).WORKING-STORAGE SECTION01 FILE-STAT PIC X(2).

Page 249: Enterprise COBOL Concepts

KSDS File Commands

OPEN INPUT file-nameOPEN OUTPUT file-name

READ file-name [NEXT] RECORD [INTO data-area] [AT END imperative] [NOT AT END imperative][END-READ]

Page 250: Enterprise COBOL Concepts

KSDS File Commands

WRITE record [FROM]] data-name] [INVALID KEY imperative] [NOT INVALID KEY imperative][END-WRITE]

REWRITE record [FROM]] data-name] [INVALID KEY imperative] [NOT INVALID KEY imperative][END-REWRITE]

CLOSE file-name

Page 251: Enterprise COBOL Concepts

KSDS File Statements

START file-name KEY IS = data-name > >= [INVALID KEY imperative] [NOT INVALID KEY imperative][END-START]

Page 252: Enterprise COBOL Concepts

KSDS File Statements

DELETE file-name RECORD

[INVALID KEY imperative]

[NOT INVALID KEY imperative]

[END-DELETE]

Page 253: Enterprise COBOL Concepts

Exercise #15

• Read BCST.SICCC01.PDSLIB(COBDATA6)• COL 1-5 KEY• COL 6-25 NAME• Allocate a KSDS with record size 25 and a key

field in cols 1-5• Write out a KSDS record for each record in the

file. Write out the records sequentially.

Page 254: Enterprise COBOL Concepts

Exercise #16

• Read BCST.SICCC01.PDSLIB(COBDATA7)

• COL 1-5 KEY

• Read a KSDS record (randomly) for each record in the file. Write out the names you find sequentially. If the record doesn’t exist, print a message “Not Found”

Page 255: Enterprise COBOL Concepts

Nested Programs

• COBOL programs can be nested. There are many advantages for doing this:

1) The monolithic working storage of most COBOL programs leads to difficulty in debugging because all data is global

2) Nested programs break the working storage into smaller areas that can only be accessed by programs that need access.

3) Nested programs provide for parameter passing by techniques found in all modern languages (by value, by reference)

4) There is no execution degradation because of nested programs. In fact, calling a nested program is more efficient than calling a separately compiled program.

Page 256: Enterprise COBOL Concepts

Nested Programs

• Calling a nested program is as efficient as performing a paragraph

• Nested programs provide design flexibility and encourage good program design

• A nested program would be called a function or subroutine in any other language

• Nested programs unleash the power of COBOL pointers and allow COBOL programmers to design data structures that encourage efficient programming techniques

Page 257: Enterprise COBOL Concepts

Nested Programs

PROGRAM-ID. MAIN.

PROGRAM-ID. SUB1.

END PROGRAM SUB1.PROGRAM-ID. SUB2END PROGRAM SUB2.END PROGRAM MAIN.

Page 258: Enterprise COBOL Concepts

COBOL Pointers

05 PTR1 USAGE IS POINTER. 05 PTR2-P USAGE IS POINTER VALUE NULL.SET PTR1 TO ADDRESS OF LINKAGE-THINGSET PTR2 TO PTR1LINKAGE SECTION. 01 NAME-STRUCTURE. 05 FIRST-NAME PIC X(18). 05 LAST-NAME PIC X(26).

SET ADDRESS OF NAME-STRUCTURE TO EXAMPLE-P.

Page 259: Enterprise COBOL Concepts

COBOL Pointers

IF PTR1 NOT = NULL AND

PTR1 NOT = PTR2

PERFORM 2730-SOMETHING

END-IF

Page 260: Enterprise COBOL Concepts

SYNCHRONIZED

• The SYNCHRONIZED clause is sometimes used with USAGE IS COMP or USAGE IS INDEX items. It is used to optimize speed of processing but it does so at the expense of increased storage requirements.

• The word SYNC can be used instead of SYNCHRONIZED

• SYNCHRONIZED causes slack bytes to be generated when needed.