HP 3000 Manuals

REPLACE Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

REPLACE Statement 

The REPLACE statement is used to replace source program text.  This
statement may appear anywhere in a program, from the IDENTIFICATION
DIVISION to the end of the PROCEDURE DIVISION. However, a better way to
edit the source text permanently is by using an editor.

Syntax 

[]
Parameters ==pseudo-text-1== a sequence of words, comments, or spaces delimited on either end by double equal signs. It may consist of any text, except that the text may not consist of null text (that is,====), all spaces, commas, semicolons, or all comment lines. _________________________________________________ NOTE Where the sequence consists only of a single element, using the identifier-1, literal-1 or word-1 format is more efficient. _________________________________________________ ==pseudo-text-2== a sequence of words, comment lines, or spaces delimited on either end by double equal signs. It can be any text, including null text (that is, ====). Character strings within pseudo-text-1 and pseudo-text-2 may be continued. However, both equal signs forming the delimiters must be on the same line. Description A REPLACE statement can appear in a source program anywhere a character string or a separator can occur. However, a REPLACE statement cannot appear in another REPLACE statement. When a REPLACE statement is used, it must be preceded by a space and terminated by a period. Any REPLACE statements contained in a source program are processed after the processing of the COPY statements contained in a source program. The text produced as a result of processing a REPLACE statement cannot contain a REPLACE statement.
NOTE Although the REPLACE statement is not sent to the compiler, it appears in the listing sent to the listfile.
To facilitate the following discussion, the REPLACE statement is rewritten as shown below. REPLACE text-to-replace BY replace-text The format 1 REPLACE statement specifies the text of the source program to be replaced by the corresponding text. Each matched occurrence of the text-to-replace in the source program is replaced by the corresponding replace-text. The format 2 REPLACE statement specifies that any text replacement currently in effect is discontinued. A given occurrence of the REPLACE statement is in effect from the point at which it is specified until the next occurrence of the REPLACE statement or the end of the separately compiled program, respectively. For example, a REPLACE statement could be in effect until the END PROGRAM header of a program that is not contained in another program is encountered. Starting with the leftmost word in the source program and the first text-to-replace, the corresponding text-to-replace is compared to an equivalent number of contiguous words in the source program. Text-to-replace matches the source program text only if each character in text-to-replace equals the character in the corresponding position of the source program text. For purposes of matching, each occurrence of a separator comma, or semicolon in text-to-replace or in the source program text, is considered to be a single space. Each sequence of one or more spaces is considered to be a single space. If no match occurs, the comparison is repeated with each successive text-to-replace (if there is any), until either a match is found or there is no successive text-to-replace. The next word of the source program text is then considered as the leftmost word of the source program text. The comparison is repeated, starting with the first text-to-replace. Whenever a match occurs between text-to-replace and source program text, replace-text is placed into the source program. The word immediately to the right of the matching source program text is then considered as the leftmost word of the data. Comparison begins again, starting with the first text-to-replace. The comparison operation continues until the rightmost word in the source program text (within the scope of the REPLACE statement) has either been matched or has been considered as a leftmost word in the source program and completed a comparison cycle. A comment line in text-to-replace or in the source program is interpreted as a single space for purposes of matching. The syntactic correctness of the lines in a source program cannot be independently determined, neither can the syntactic correctness of the entire COBOL source program be determined until all REPLACE statements have been completely processed. The following are examples using the REPLACE statement. Example 1 In this example, the 01 identifier "TEST" is replaced, but not the "TEST" in the PERFORM statement. This is due to the intervening REPLACE statement. A better way to produce the same result is to edit the source text permanently using an editor. Before REPLACE is executed: IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. DATA DIVISION. REPLACE ==TEST== BY ==TESTT== ==TRUE== BY ==TRUE-FLAG==. 01 NAME PIC X(30). 01 TEST PIC X. 88 TRUE VALUE "T". PROCEDURE DIVISION. P1. ACCEPT TEST. IF TRUE PERFORM P2. REPLACE ==ALPHABETIC== BY ==ALPHABETIC-UPPER==. IF NAME IS ALPHABETIC THEN SET TRUE-FLAG TO TRUE. REPLACE OFF. PERFORM P3 WITH TEST AFTER UNTIL NAME IS NOT ALPHABETIC. : The code sent to the compiler would be: IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. DATA DIVISION. 01 NAME PIC X(30). 01 TESTT PIC X. 88 TRUE-FLAG VALUE "T". PROCEDURE DIVISION. P1. ACCEPT TESTT. IF TRUE-FLAG PERFORM P2. IF NAME IS ALPHABETIC-UPPER THEN SET TRUE-FLAG TO TRUE. PERFORM P3 WITH TEST AFTER UNTIL NAME IS NOT ALPHABETIC. : Example 2 [REV BEG] This example shows how the REPLACE statement can be used to replace substrings. This is done by putting parentheses around the substring to be copied and around pseudo-text-1. Assume the source program contains the following text before the replacement:[REV END] 01 (FIRST)-RECORD PIC X(80). [REV BEG] After the following REPLACE statement[REV END] REPLACE COPY-MODULE REPLACING ==(FIRST)== BY ==INPUT==. the resultant text is: 01 INPUT-RECORD PIC X(80).


MPE/iX 5.0 Documentation