INSPECT

The INSPECT statement can be used to tally the number of occurrences of specific character strings, to replace characters by other characters, or to convert from one set of characters to another including UPPERCASE TO LOWERCASE and vice versa. The INSPECT verb has two options, TALLYING and REPLACING. You can do one or the other or both. If both are done, the TALLYING is done before the replacing.

In using the TALLYING format of the INSPECT, you are tallying into a field that is a counter. This field must be initialized to ZERO before inspect is done.

INSPECT Statements

  • INSPECT statement with TALLYING phrase (Key Phrases to use: BEFORE/AFTER, CHARACTERS, ALL, LEADING and FIRST)
  • INSPECT statement with REPLACING phrase (Key Phrases to use: BEFORE/AFTER, CHARACTERS BY, ALL, LEADING and FIRST)
  • INSPECT statement with TALLYING and REPLACING phrases
  • INSPECT statement with CONVERTING phrase

INSPECT Examples

Example #1: This statement counts the number of occurrences of the letter ‘C’ until the first space is encountered. If WS-INPUT = AB CC DCF C Y then after the INSPECT, WS-CNTR would equal 0.

MOVE 0 TO WS-CNTR.
 INSPECT WS-INPUT TALLYING WS-CNTR
     FOR ALL "C" BEFORE INITIAL SPACE.

This statement counts the number of occurrences of the letter ‘C’ after the first space is encountered. If WS-INPUT = AB CC DCF C Y then after the INSPECT, WS-CNTR would equal 4.

INSPECT WS-INPUT TALLYING WS-CNTR
     FOR ALL "C" AFTER INITIAL SPACE.

Example #2: This statement counts the number of spaces in the field WS-INPUT.

If WS-INPUT = AB CC DCF C Y then after the INSPECT, WS-CNTR would equal 4 since there is a space between each letter.

MOVE 0 TO WS-CNTR.
INSPECT WS-INPUT TALLYING WS-CNTR
    FOR ALL SPACES.

Example #3: In this example, the count of all characters before the first space is tallied. If WS-INPUT is equal to AB CC DCF C Y then WS-CNTR will contain the number 2 since A and B proceed the space before the C.

MOVE 0 TO WS-CNTR.
INSPECT WS-INPUT TALLYING WS-CNTR
    FOR CHARACTERS BEFORE INITIAL SPACE.

Example #4: This statement counts all of the leading zeros in a field. Embedded or trailing zeros are counted. WS-INPUT is equal to 00010050999, then WS-CNTR is equal to 3.

MOVE 0 TO WS-CNTR.
INSPECT WS-INPUT TALLYING WS-CNTR
    FOR LEADING ZEROS.

Example #5: This statement tallies for all zeroes that proceed the initial 2 In this case, WS-CNTR will be 5. WS-INPUT is equal to 00010050999.

INSPECT WS-INPUT TALLYING WS-CNTR
    FOR ALL ZEROS BEFORE INITIAL 5.

Example #6: This statement will replace all B with G. WS-INPUT will start as ABCBDFB and will become AGCGDFG.

INSPECT FLDH REPLACING ALL "B" BY "G".

Example #7: This replace will change all A to X. This means that AAABBAAA will become XXXBBAAA.

INSPECT WS-INPUT REPLACING CHARACTERS BY "X" 
        BEFORE INITIAL "B".

Example #8: This will replace the first X with a 5. There ACXDGXB will become AC5DGXB.

INSPECT WS-INPUT REPLACING FIRST "X" BY "5".

Example #9: 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    DATA-2 after
 00ACADEMY00    2   00AC2DEMY00
 0000ALABAMA    4   0000ALABAMA
 CHATHAM0000    0   CH2THAM0000

Example #10: UPPER CASE TO LOWER CASE- This can be used to convert the upper case letters to lower case and vice versa but here the upper case and lower case letters needs to  be declared in working storage as shown below.

01 WS-CHAR-CASE.
    05 WS-UPPER-CASE PIC X(26) VALUE "ABCD … Z"
    05 WS-LOWER-CASE PIC X(26) VALUE ' abcd… z'

 INSPECT WR-IN-STRNG
          CONVERTING WW-LOWER-CASE to WS-UPPER-CASE

COBOL Blog: Click Here IBM Reference:Click Here

Scroll to Top