The following examples show some uses of the INSPECT statement to examine and replace characters.
In the following example, the INSPECT statement examines and replaces characters in data item DATA-2. The number of times a leading zero (0) occurs in the data item is accumulated in COUNTR. The first instance of the character A that follows the first instance of the character C is replaced by the character 2.
77 COUNTR PIC 9 VALUE ZERO.
01 DATA-2 PIC X(11).
. . .
INSPECT DATA-2
TALLYING COUNTR FOR LEADING "0"
REPLACING FIRST "A" BY "2" AFTER INITIAL "C"
| DATA-2 before | COUNTR after | DATA-2 after |
|---|---|---|
| 00ACADEMY00 | 2 | 00AC2DEMY00 |
| 0000ALABAMA | 4 | 0000ALABAMA |
| CHATHAM0000 | 0 | CH2THAM0000 |
In the following example, the INSPECT statement examines and replaces characters in data item DATA-3. Each character that precedes the first instance of a quotation mark (") is replaced by the character 0.
77 COUNTR PIC 9 VALUE ZERO.
01 DATA-3 PIC X(8).
. . .
INSPECT DATA-3
REPLACING CHARACTERS BY ZEROS BEFORE INITIAL QUOTE
| DATA-3 before | COUNTR after | DATA-3 after |
|---|---|---|
| 456"ABEL | 0 | 000"ABEL |
| ANDES"12 | 0 | 00000"12 |
| "TWAS BR | 0 | "TWAS BR |
The following example shows the use of INSPECT CONVERTING with AFTER and BEFORE phrases to examine and replace characters in data item DATA-4. All characters that follow the first instance of the character / but that precede the first instance of the character ? (if any) are translated from lowercase to uppercase.
01 DATA-4 PIC X(11).
. . .
INSPECT DATA-4
CONVERTING
"abcdefghijklmnopqrstuvwxyz" TO
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
AFTER INITIAL "/"
BEFORE INITIAL"?"
| DATA-4 before | DATA-4 after |
|---|---|
| a/five/?six | a/FIVE/?six |
| r/Rexx/RRRr | r/REXX/RRRR |
| zfour?inspe | zfour?inspe |