COBOL Manual for TNS/E Programs (H06.08+, J06.03+)

Usage Considerations:
Rules for Replacement
CHARACTERS causes each character in source-string to be replaced by
replace-string.
ALL causes each occurrence of compare-string in source-string to be replaced
by replace-string.
LEADING causes each contiguous occurrence of compare-string in source-string
to be replaced by replace-string, provided that the leftmost occurrence is at the
point where comparison began in the first comparison cycle in which compare-string was
eligible to participate.
FIRST causes the leftmost occurrence of compare-string in source-string to be
replaced by replace-string.
ALL, LEADING, and FIRST apply to each succeeding BY phrase until the next ALL,
LEADING, or FIRST appears.
See these usage considerations in INSPECT TALLYING (page 345):
How Parameter Categories Affect INSPECT
Comparison Operation
BEFORE and AFTER Phrases
Leading Zeros
HP COBOL does not treat leading spaces in a numeric data item as if they were leading zeros,
as some implementations of COBOL do. To convert leading spaces to zeros, use the INSPECT
statement as Example 108 does.
Example 108 INSPECT Statement Converting Leading Spaces to Zeros
WORKING-STORAGE SECTION.
03 A-VALUE PICTURE X(10) VALUE " 23".
03 N-VALUE PICTURE 9(10) REDEFINES A-VALUE.
...
INSPECT A-VALUE REPLACING LEADING " " BY "0".
In Example 109, several replacements occur during the execution of one INSPECT statement.
When the INSPECT statement finishes executing, ITEM-C equals “$$9,XXX.00.
Example 109 One INSPECT Statement, Several Replacements
WORKING-STORAGE SECTION.
77 ITEM-C PIC X(10) VALUE "009,999. ".
PROCEDURE DIVISION.
INSPECT ITEM-C REPLACING ALL " " BY "0",
LEADING "0" BY "$",
ALL "9" BY "X" AFTER INITIAL ","
INSPECT TALLYING REPLACING
INSPECT TALLYING REPLACING counts occurrences of a sequence of one or more characters in
a data item and replaces each occurrence with a specified value.
352 Procedure Division Verbs