using the copy, call, string , unstring statements

34
16 -1 Chapter 16

Upload: tobias-gregory

Post on 01-Jan-2016

16 views

Category:

Documents


0 download

DESCRIPTION

Chapter 16. Using The COPY, CALL, STRING , UNSTRING STATEMENTS. Chapter Objectives. To familiarize you with COPY statement for copying parts of a program stored in a library CALL statement for executing called programs as subroutines Text manipulation with STRING and UNSTRING statements. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

16-1

Chapter 16

Page 2: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To familiarize you with COPY statement for copying parts of a

program stored in a library CALL statement for executing called

programs as subroutines Text manipulation with STRING and

UNSTRING statements

16-2

Page 3: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To copy prewritten COBOL entries stored in a library into a program

Often used to copy ◦ FD, 01 entries that define and describe files and

records◦ Tables◦ SCREEN SECTIONs◦ Standard modules used in PROCEDURE DIVISION

Like the #include in C++

16-3

Page 4: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

COPY test-name OF

library-name.

IN

Use in ENVIRONMENT, DATA, or PROCEDURE DIVISION

Library-name is external-name, 1 to 8 characters, letters and digits only

We’ll have to check to see how to create library on the alpha

Can be used to replace with other text also using the REPLACE keyword.

16-5

Format

Page 5: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Suppose file called Customer, stored in library contains following entries:01 Customer-Rec.

05 Cust-No Pic X(5).05 Cust-Name Pic X(20).05 Cust-Address Pic X(30).05 Cust-Bal-Due Pic 9(4)V99.

16-6

Page 6: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Code COPY statement at point in program where entries should appear

Data Division.File Section.FD CustFile.Copy Customer.

In source listing, copied lines are marked with a C or other designator

16-7

Page 7: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

… ...11 FD CustFile.12 Copy Customer.13C 01 Customer-Rec.14C 05 Cust-No Pic X(5).15C 05 Cust-Name Pic X(20).16C 05 Cust-Address Pic X(30).17C 05 Cust-Bal-Due Pic 9(4)V99.

16-8

Line numbers followed by C are copied statements

Page 8: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Used in PROCEDURE DIVISION to CALL or reference independent subprograms stored in library

Calling program - main program that calls or references subprogram

Called program - subprogram stored in separate file that is linked and executed within main program

16-11

Page 9: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Compiled, debugged, stored in library to be called when needed

Typical subprograms include:◦ Edit routines◦ Error control checks◦ Standard calculations◦ Summary, total printing

16-12

Page 10: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

CALL literal-1[USING identifier-1 …]

To call subprogram into main program literal-1 is PROGRAM-ID name in called

program Enclose in quotes

16-16

Format

Page 11: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Calling and Called Programs

Identification Division....Procedure Division....

Call 'Sub1'...

Call 'Sub2'...

Identification Division.Program-ID. Sub1.…Procedure Division. Exit

Program.

16-17

Identification Division.

Program-ID. Sub2.…Procedure Division.

Exit Program.

1

2

3

4

Page 12: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

USING clause required if data passed between calling and called program

Parameter passed in several ways:◦ Value of parameter may be passed to and used

by subprogram but not changed◦ Value for parameter may be calculated by

instructions in subprogram and passed back to main program

16-19

Page 13: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Suppose a main program ◦ Reads in employee records◦ Produces report showing employee salary, Social

Security tax and Medicaid tax (taxes referred to as FICA taxes)

Subprogram is to calculate amount of each tax based on the salary field◦ Assume PROGRAM-ID is FICAProg

16-20

Page 14: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Parameters needed ◦ Salary (WS-Ann-Sal)

Passed to subprogram, used to calculate taxes but not changed by subprogram

◦ Tax fields Soc-Sec and Med-Tax Subprogram calculates values for these parameters

and passes them back to main program

16-21

Page 15: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

CALL statement with:◦ Literal same as PROGRAM-ID of called program◦ List of parameters in USING clause

Call 'FICAProg' Using WS-Ann-Sal, Soc-Sec, Med-Tax

16-22

Example

Page 16: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

PROGRAM-ID identifier is literal used in CALL in main program

LINKAGE SECTION must be defined in DATA DIVISION◦ Follows FILE and WORKING-STORAGE SECTIONs◦ Describes all items passed between main and

subprogram◦ Format same as WORKING-STORAGE

16-23

Page 17: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

PROCEDURE DIVISION header must include USING clause◦ Lists all parameters or fields defined in LINKAGE

SECTION EXIT PROGRAM

◦ Last executed statement in called program◦ Returns control back to calling program

16-24

Page 18: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Identification Division.Program-ID. FICAProg.Data Division.Linkage Section.01 WS-Ann-Sal Pic 9(6).01 Soc-Sec Pic 9(4)V99.01 Med-Tax Pic 9(5)V99.

16-25

Page 19: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Procedure Division Using WS-Ann-Sal, Soc-Sec, Med-Tax.

If WS-Ann-Sal <= 84900Compute Soc-Sec = .062 * WS-Ann-Sal

ElseCompute Soc-Sec = .062 * 84900

End-IfCompute Med-Tax = .0145 * WS-Ann-SalExit Program.

16-26

Page 20: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

When FICAProg is called◦ Value of WS-Ann-Sal passed to subprogram◦ Value of Soc-Sec and Med-Tax undefined

When FICAProg finished◦ Values calculated for Soc-Sec and Med-Tax

passed back to main program

16-27

Page 21: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Parameters passed in sequence◦ First parameter in CALL … USING passed to first

parameter in PROCEDURE DIVISION USING clause and so on

Number of parameters in both USING clause should be the same

PIC clauses for corresponding parameters must be same

Same as you are used to in other languages

16-28

Page 22: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To combine or concatenate several fields into one

Consider following entries:05 Name.

10 Last-Name Pic X(10).10 First-Name Pic X(10).10 Middle-Name Pic X(6).

16-31

Page 23: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Suppose name fields have values below

Print name with single blank between parts: THOMAS ALVA EDISON

16-32

Last-NameE D I S O N

First-NameT H O M A S

Middle-NameA L V A

Page 24: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

STRING identifier-1 ...literal-1

identifier-2DELIMITED BY literal-2 ...

SIZEINTO identifier-3

[END-STRING]

16-33

Page 25: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

identifier-1 or literal-1 is field or value to be combined

identifier-3 is field in which all identifiers or literals are combined

DELIMITED BY clause◦ Transmitting of characters ends when value of

identifier-2 or literal-2 encountered◦ SIZE means entire contents to be copied

16-34

Page 26: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

StringFirst-Name Delimited By ' 'Middle-Name Delimited By ' 'Last-Name Delimited By ' 'Into Name-Out

Copies characters from named fields to Name-Out up to first blank in field

Name-Out is THOMASALVAEDISON

16-35

Page 27: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To insert a blank between parts of name

StringFirst-Name Delimited By ' '' ' Delimited By SizeMiddle-Name Delimited By ' '' ' Delimited By SizeLast-Name Delimited By ' 'Into Name-Out

16-36

Places blank after each field

Page 28: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Specifies operation(s) to be performed if receiving field not large enough to accommodate result

NOT ON OVERFLOW option may also be used

END-STRING scope terminator also available

16-37

Page 29: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To count number of characters moved in STRING statement

Increments specified field by one for every character moved

If you need to know how may characters were concatenated

Tells you where you are now pointing within the string

16-38

Page 30: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Move 1 To WS-CountString First-Name Delimited By ' '

Into Name-OutWith Pointer WS-Count

If First-Name is 'Paul', WS-Count is five after STRING

Subtract one from WS-Count to get length of actual move (4)

16-39

Page 31: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Also used to move data to receiving field beginning at some point other than first position

If WS-Count initialized to 15 before STRING, First-Name moved to Name-Out beginning with fifteenth position of Name-Out

16-40

Page 32: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

To separate a field into its components Suppose Name-In equals TAFT, WILLIAM, H The last name, first name and middle initial

can be stored in separate fields without commas with the following UNSTRING statement

16-42

Page 33: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Unstring Name-InDelimited By ','

Into Last-NameFirst-NameMiddle-Initial

TAFT will be stored in Last-Name, William in First-Name and H in Middle-Initial

16-43

Page 34: Using  The COPY, CALL,  STRING , UNSTRING STATEMENTS

Sending field, as well as literal, must be nonnumeric

Receiving fields may be numeric or nonnumeric

ALL phrase means one or more occurrences of literal or identifier treated as one

POINTER and OVERFLOW clauses may also be used

16-45