Comparison and replacement examples
This topic shows examples of comparison and replacement.
Sequences of code (such as file and data descriptions, error, and exception routines) that are common to a number of programs can be saved in a library, and then used with the COPY statement. If naming conventions are established for such common code, the REPLACING phrase need not be specified. If the names change from one program to another, the REPLACING phrase can be used to supply meaningful names for this program.
Example 1
In this
example, the library text PAYLIB
consists of the
following DATA DIVISION entries:
01 A.
02 B PIC S99.
02 C PIC S9(5)V99.
02 D PIC S9999 OCCURS 1 TO 52 TIMES
DEPENDING ON B OF A.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 A.
02 B PIC S99.
02 C PIC S9(5)V99.
02 D PIC S9999 OCCURS 1 TO 52 TIMES
DEPENDING ON B OF A.
Example 2
To change some or all of the names within the library text, you can use the REPLACING phrase:
COPY PAYLIB REPLACING A BY PAYROLL
B BY PAY-CODE
C BY GROSS-PAY
D BY HOURS.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAY-CODE PIC S99.
02 GROSS-PAY PIC S9(5)V99.
02 HOURS PIC S9999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAY-CODE OF PAYROLL.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.
Example 3
If the following conventions are followed in the library text, parts of names (for example, the prefix portion of data names) can be changed with the REPLACING phrase.
In this example, the library text PAYLIB
consists
of the following DATA DIVISION entries:
01 :TAG:.
02 :TAG:-WEEK PIC S99.
02 :TAG:-GROSS-PAY PIC S9(5)V99.
02 :TAG:-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON :TAG:-WEEK OF :TAG:.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB REPLACING ==:TAG:== BY ==Payroll==.
Usage Note: In this example, the required use of colons or parentheses as delimiters in the library text. Colons are recommended for clarity because parentheses can be used for a subscript, for instance in referencing a table element.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.
Example 4
This example shows how to selectively replace level numbers without replacing the numbers in the PICTURE clause:
COPY xxx REPLACING ==(01)== BY ==(01)==
== 01 == BY == 05 ==.
Example 5
This
example demonstrates use of the LEADING keyword of the REPLACING phrase
in the COPY statement. The library text PAYLIB
consists
of the following DATA DIVISION entries:
01 DEPT.
02 DEPT-WEEK PIC S99.
02 DEPT-GROSS-PAY PIC S9(5)V99.
02 DEPT-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON DEPT-WEEK OF DEPT.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB REPLACING LEADING == DEPT == BY == PAYROLL ==.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.
Example 6
This
example demonstrates use of the TRAILING keyword of the REPLACING
phrase in the COPY statement. The library text PAYLIB
consists
of the following DATA DIVISION entries:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB REPLACING TRAILING == GROSS-PAY == BY == NET-PAY ==.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-NET-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.
Example 7
This
example demonstrates a scenario where two types of partial-word replacement
are specified in a single REPLACING phrase. The library text PAYLIB
consists
of the following DATA DIVISION entries:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 :TAG:-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB REPLACING == :TAG: == BY == PAYROLL ==
TRAILING == GROSS-PAY == BY == NET-PAY ==.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
Two types of partial-word replacement are specified
in the same REPLACING operation, but as usual, one replacement is
done on a single library text word. Therefore, even though :TAG:-GROSS-PAY
is
considered a match with the first operand of both replacement operations,
after the first match and replacement is performed, no more replacement
is performed on that word.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.
Example 8
This example demonstrates support for the REPLACING phrase in a chain of nested COPY statements. In this example, it is the outermost COPY statement that has the REPLACING phrase, but note that the REPLACING phrase can be specified on any of the nested COPY statements in the chain, provided it is specified on only one of them. The library text PAYLIB consists of the following DATA DIVISION entries:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 :TAG:-GROSS-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
COPY PAYLIB2
The library text PAYLIB2 consists of the following DATA DIVISION entries:
01 PAYROLL2.
02 PAYROLL2-WEEK PIC S99.
02 :TAG:2-GROSS-PAY PIC S9(5)V99.
02 PAYROLL2-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL2-WEEK OF PAYROLL2.
You can use the COPY statement in the DATA DIVISION of a program as follows:
COPY PAYLIB REPLACING == :TAG: == BY == PAYROLL ==
TRAILING == GROSS-PAY == BY == NET-PAY ==.
In this program, the library text is copied. The resulting text is treated as if it were written as follows:
01 PAYROLL.
02 PAYROLL-WEEK PIC S99.
02 PAYROLL-NET-PAY PIC S9(5)V99.
02 PAYROLL-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL-WEEK OF PAYROLL.
01 PAYROLL2.
02 PAYROLL2-WEEK PIC S99.
02 PAYROLL2-NET-PAY PIC S9(5)V99.
02 PAYROLL2-HOURS PIC S999 OCCURS 1 TO 52 TIMES
DEPENDING ON PAYROLL2-WEEK OF PAYROLL2.
The REPLACING phrase in the outermost COPY statement applies not only to the library text in PAYLIB but also to the text in PAYLIB2.
The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.