INSPECT REPLACING and CONVERTING
The other half of INSPECT writes. It can replace one literal with another wherever it appears, and it can translate a set of characters into another set, which is how a COBOL program changes case without a loop or a function.
REPLACING ALL
A sentence with the word CAT in it, and the same sentence after every occurrence has become DOG:
IDENTIFICATION DIVISION.
PROGRAM-ID. REPLACE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT".
01 CODE-F PIC X(10) VALUE "AbC-123-Xy".
PROCEDURE DIVISION.
INSPECT TEXT-F REPLACING ALL "CAT" BY "DOG".
DISPLAY "[" TEXT-F "]".
INSPECT CODE-F CONVERTING "ABC" TO "abc".
DISPLAY "[" CODE-F "]".
INSPECT TEXT-F REPLACING LEADING "T" BY "t".
DISPLAY "[" TEXT-F "]".
INSPECT TEXT-F REPLACING FIRST "A" BY "@".
DISPLAY "[" TEXT-F "]".
STOP RUN.
cobc -x -free -Wall -o replace replace.cob
./replace
[THE DOG SAT ON THE MAT]
[abc-123-Xy]
[tHE DOG SAT ON THE MAT]
[tHE DOG S@T ON THE MAT]
The first line is the replacement, and the field kept its length: three characters went out and three came in, so every position after the word is where it was. A replacement of a different length is not allowed, and the fourth chapter shows the error.
CONVERTING
CONVERTING takes two strings of the same length and works character by character: a character found in the first string becomes the character in the same position of the second. The second line of the run changed A, B and C to lowercase and left the rest alone, which is why the X and the Y are still capitals.
A whole alphabet in the first string converts a whole alphabet, and that is the usual way a COBOL program upper-cases a name:
IDENTIFICATION DIVISION.
PROGRAM-ID. UPCASE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WORD-F PIC X(8) VALUE "London".
PROCEDURE DIVISION.
INSPECT WORD-F CONVERTING "abcdefghijklmnopqrstuvwxyz"
TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
DISPLAY "[" WORD-F "]".
STOP RUN.
cobc -x -free -Wall -o upcase upcase.cob
./upcase
[LONDON ]
The two spaces belong to the field, not to the word. A function would do the same job in one line of the procedure division (UPPER-CASE comes later in the course), and CONVERTING is the version that a 1970s program uses and that a compiler from any vendor implements.
LEADING and FIRST
Without a word in front of the literal, REPLACING replaces every occurrence. FIRST replaces one, and LEADING replaces the one at the start of the field, if there is one.
The third line of the run is the T of THE turned into t, and the T of THE further along left alone, because the scan found the first T at position one and stopped. The fourth line is a FIRST of a different character: the first A after the replacements is the one in SAT, so the field becomes S@T.
A leading space is the common use of LEADING: a program that wants to strip the blanks in front of a value replaces one at a time, or replaces ALL SPACES BY ZERO when zero is acceptable as a filler. There is no "replace with nothing" in INSPECT, which is why a trim is written with a move or with FUNCTION TRIM.
The two operands must be the same size
A replacement cannot change the length of the field, so the literal before BY and the literal after it have to be the same size:
IDENTIFICATION DIVISION.
PROGRAM-ID. RULES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT".
PROCEDURE DIVISION.
INSPECT TEXT-F REPLACING LEADING "THE" BY "A".
DISPLAY TEXT-F.
STOP RUN.
cobc -x -free -Wall -o rules rules.cob
rules.cob:9: error: REPLACING operands differ in size
7 |
8 | PROCEDURE DIVISION.
9 > INSPECT TEXT-F REPLACING LEADING "THE" BY "A".
10 | DISPLAY TEXT-F.
11 | STOP RUN.
The rule is the reason the statement is fast: the field is a fixed rectangle of bytes and a replacement is a write of the same width. A program that has to change the length of a line uses STRING to build a new one, or UNSTRING to take the old one apart.
Exercises
- Replace every "A" in the sentence with "*" and print it. How many replacements were made?
- Convert the sentence to capitals with CONVERTING and print it. Which characters changed, and which did not?
- Write a replacement of "THE" by "A" that the compiler accepts. What does the field look like, and how did you keep the length?
- Use INSPECT ... TALLYING to count the spaces and REPLACING to turn them into full stops in the same program. Which order of the two statements gives which result?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does INSPECT ... REPLACING ALL change?
Answer
Every occurrence of the literal, in place, at the same length
REPLACING overwrites the same number of bytes at every match, which is why the two literals must be the same size.
-
What does CONVERTING do?
Answer
It maps each character found in the first string to the character in the same position of the second
It is a character-by-character translation, so an alphabet in the first string changes an alphabet.
-
Which form replaces only the occurrence at the start of the field?
Answer
REPLACING LEADING
LEADING requires the match at the start; FIRST takes the first match anywhere.
-
Why must the two literals of a REPLACING be the same size?
Answer
The field is a fixed width and the statement writes in place
A same-width write keeps every following byte where it was; changing the length is the job of STRING and UNSTRING.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". PROCEDURE DIVISION. INSPECT TEXT-F REPLACING ALL "CAT" BY "DOG". DISPLAY "[" TEXT-F "]". STOP RUN.Answer
[THE DOG SAT ON THE MAT]
Only the three characters of CAT are overwritten, and the rest of the twenty-two bytes stay where they were.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-F PIC X(10) VALUE "AbC-123-Xy". PROCEDURE DIVISION. INSPECT CODE-F CONVERTING "ABC" TO "abc". DISPLAY "[" CODE-F "]". STOP RUN.Answer
[abc-123-Xy]
Only A, B and C are in the first string, so only they are translated; the X and Y keep their case.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". PROCEDURE DIVISION. INSPECT TEXT-F REPLACING LEADING "T" BY "t". DISPLAY "[" TEXT-F "]". STOP RUN.Answer
[tHE CAT SAT ON THE MAT]
LEADING matches only at the start of the field, so the second THE is untouched.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". PROCEDURE DIVISION. INSPECT TEXT-F REPLACING ALL "T" BY "t". DISPLAY "[" TEXT-F "]". STOP RUN.Answer
[tHE CAt SAt ON tHE MAt]
Without a limit every T is replaced, and the sentence has five: THE, CAT, SAT, THE and MAT.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 WORD-F PIC X(8) VALUE "London". PROCEDURE DIVISION. INSPECT WORD-F CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ". DISPLAY "[" WORD-F "]". STOP RUN.Answer
[LONDON ]
Every lowercase letter is mapped to its capital, and the two unused positions of the field stay spaces.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEXT-F PIC X(22) VALUE "THE CAT SAT ON THE MAT". PROCEDURE DIVISION. INSPECT TEXT-F REPLACING LEADING "THE" BY "A". DISPLAY TEXT-F. STOP RUN.Answer
error: REPLACING operands differ in size
The statement writes in place, so the two literals have to occupy the same number of bytes.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev