COBOL Manual for TNS and TNS/R Programs
Source Text Manipulation
HP COBOL Manual for TNS and TNS/R Programs—522555-006
10-16
REPLACE Statement
•
Persistence
A given occurrence of the REPLACE statement is active from the point at which
you specify it until the next occurrence of a REPLACE statement or the end of the
separately compiled program, respectively.
•
REPLACE Statements and the Listing
The listing the compiler produces shows the lines containing REPLACE statements
like any other lines. When any replacement occurs, the image in the listing is that
of the text after replacement.
•
Order of Processing
Conceptually, the compiler processes any REPLACE statements in a source
program after it has processed any COPY statements.
•
Comparison Operation
The compiler determines which sequences of text-words to replace by comparing
pseudo-text-1 to text-words in the source text. This is how the comparison
operation works:
1. Starting with the leftmost source program text-word and the first pseudo-
text-1, the compiler compares the word or words of pseudo-text-1 with
the equivalent number of contiguous text-words in the source program.
2. During the comparison, the compiler handles each occurrence of a separator
comma or semicolon and each sequence of one or more space separators as
a single space. The compiler ignores any comment or directive line in the
source text or in pseudo-text-1. The pseudo-text-1 matches the source
text if the two sequences of text-words are equal, character for character.
3. If no match occurs, the compiler repeats the comparison with each successive
occurrence of pseudo-text-1, if any, in the REPLACE statement until a
match occurs or until it has compared each operand in the pseudo-text-1
to the leftmost source program text-word or text-words.
4. If the compiler compares all the occurrences of pseudo-text-1 without
finding a match, it considers the next text-word of the source program as the
leftmost source program text-word and starts the comparison cycle again with
the first occurrence of pseudo-text-1.
5. Whenever a match occurs between pseudo-text-1 and the source program
text, the compiler replaces the matched text in the source program with the
corresponding pseudo-text-2. The compiler then considers the source
program text-word immediately following the rightmost text-word that
participated in the match to be the leftmost source program text-word, and
starts the comparison cycle again with the first occurrence of pseudo-
text-1.