Examples: INSPECT statement

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 first instance of the character A that follows the first instance of the character C is replaced by the character 2.


01  DATA-2            PIC X(11).
. . .
    INSPECT DATA-2
      REPLACING FIRST "A" BY "2" AFTER INITIAL "C"

DATA-2 before DATA-2 after
00ACADEMY00 00AC2DEMY00
0000ALABAMA 0000ALABAMA
CHATHAM0000 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.


01  DATA-3            PIC X(8).
. . .
    INSPECT DATA-3
      REPLACING CHARACTERS BY ZEROS BEFORE INITIAL QUOTE

DATA-3 before DATA-3 after
456"ABEL 000"ABEL
ANDES"12 00000"12
"TWAS BR "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