Summary -

The INSPECT statement examines characters or groups of characters in a data item. INSPECT statement used to do the following tasks by using its four formats -

  1. INSPECT TALLYING: Counts the specific character or set of characters occurrences in a data item.
  2. INSPECT REPLACING: Replaces all or portions of a data item with specified characters.
  3. INSPECT TALLYING REPLACING: Counts the specific characters occurrences and Replaces all or portions of a data item with specified characters such as spaces or zeros.
  4. INSPECT CONVERTING: Converts all occurrences of specific characters in a data item to user-supplied replacement characters.

INSPECT TALLYING -

Counts the specific character or set of characters occurrences in a data item. The counter data type should be numeric.

Syntax -

INSPECT TALLYING Syntax

TALLYING Phase -

Counts the occurrences of a specific character or special character in a data item. Let’s take below example to explain the work flow -

IDENTIFICATION DIVISION.
PROGRAM-ID. INSPTALY.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION. 
01 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
01 WS-CNT               PIC 9(02).
PROCEDURE DIVISION. 
    DISPLAY "INSPECT TALLYING....". 
    INSPECT WS-DATA TALLYING WS-CNT FOR ALL "A". 
    DISPLAY "COUNT OF A LETTER IS : "WS-CNT.
    STOP RUN. 

The below diagram explains how the INSPECT counts the number of occurrences of 'A'.

Diagram -

INSPECT Diagram

The INSPECT validation performs in iterations and the number of iterations is exactly equal to the length of the data item. In the above example, the length of the data item is 10. So, the number of iterations are 10. The validation starts from 1st byte and the length of TALLYING character is one.

In the first iteration, the first character 'M' compared with TALLYING character 'A'. The result is False and the counter (WS-CNT) doesn't increase. In the second iteration, the second character 'A' compared with TALLYING character 'A'. The result is True and the counter (WS-CNT) increased by 1.

Similarly, every character compared with TALLYING character 'A' and the counter (WS-CNT) gets increased when the result is true. The result counter (WS-CNT) value is 2 at the end.

INSPECT REPLACING -

Replaces all or portions of a data item with specified characters.

Syntax -

INSPECT REPLACING Syntax

Let’s take the below example to explain the work flow -

IDENTIFICATION DIVISION.
PROGRAM-ID. INSPREPL. 
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION. 
01 WS-DATA		PIC X(10) VALUE "DD-MM-YYYY".
PROCEDURE DIVISION.
    DISPLAY "INSPECT REPLACING....".  
    INSPECT WS-DATA REPLACING ALL "-" BY "/".
    DISPLAY "DATA AFTER REPLACING : "WS-DATA.
    STOP RUN.

Diagram -

INSPECT REPLACING Diagram

The INSPECT validation performs in iterations and the number of iterations is exactly equal to the length of the data item. In the above example, the length of the data item is 10. So, the number of iterations are 10. The validation starts from 1st byte and the length of REPLACING character is one.

In the first iteration, the first character 'D' compared with REPLACING character '-'. The result is False and the character won’t get replaced. In the second iteration, the second character 'D' compared with REPLACING character '-'. The result is False and the character won’t get replaced.

In the third iteration, the third character '-' compared with REPLACING character '-'. The result is true and the character gets replaced with '/'. Similarly, every character compared with REPLACING character '-' and the character gets replaced with '/' when the result is true.

INSPECT TALLYING REPLACING -

Counts the specific characters occurrences and Replaces all or portions of a data item with specified characters such as spaces or zeros. INSPECT TALLYING REPLACING used to count and replace the occurrence of single character as well as set of characters. INSPECT TALLYING REPLACING performs the operations like TALLYING first and REPLACING next.

Syntax -

INSPECT TALLYING REPLACING Syntax

Let’s take below example to explain the work flow -

IDENTIFICATION DIVISION.
PROGRAM-ID. INSPTARP. 
ENVIRONMENT DIVISION. 
DATA DIVISION. 
WORKING-STORAGE SECTION.
01 WS-DATA		PIC X(10) VALUE "DD-MM-YYYY".
01 WS-CNT 	 	PIC 9(02) VALUE ZEROES. 
PROCEDURE DIVISION.
    DISPLAY "INSPECT TALLYING REPLACING....". 
    INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-" 
    REPLACING ALL "-" BY "/". 
    DISPLAY "DATA AFTER TALLYING REPLACING : "WS-DATA. 
    DISPLAY "COUNT OF - LETTER IS : "WS-CNT.
    STOP RUN.

Diagram -

Inspect tallying replacing workflow

The INSPECT validation performs in iterations and the number of iterations is exactly equal to the length of the data item. In the above example, the length of the data item is 10. So, the number of iterations are 10. The validation starts from 1st byte and the length of TALLYING and REPLACING character is one.

In the first iteration, the first character 'D' compared with TALLYING character '-'. The result is False and the counter (WS-CNT) doesn't increase. The first character 'D' compared with REPLACING character '-'. The result is False and the character won’t get replaced.

In the second iteration, the second character 'D' compared with TALLYING character '-'. The result is False and the counter (WS-CNT) doesn't increase. The first character 'D' compared with REPLACING character '-’. The result is False and the character won’t get replaced.

In the third iteration, the third character '-' compared with TALLYING character '-'. The result is true and the counter (WS-CNT) gets increased by 1. The first character 'D' compared with REPLACING character '-’. The result is true and the character gets replaced with '/'.

Similarly, every character compared with TALLYING and REPLACING character '-' and the counter (WS-CNT) gets increased, the character gets replaced with '/' when the result is true. The result counter (WS-CNT) value is 2 at the end.

INSPECT CONVERTING -

Converts all occurrences of specific characters in a data item to user-supplied replacement characters. INSPECT CONVERTING works on individual characters in the group of characters.

Syntax -

INSPECT Converting Syntax

Let’s take below example to explain the work flow -

IDENTIFICATION DIVISION. 
PROGRAM-ID. INSPCONV. 
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DATA		PIC X(10) VALUE "MAINFRAMES".
PROCEDURE DIVISION.  
    DISPLAY "INSPECT TALLYING REPLACING....".
    INSPECT WS-DATA CONVERTING 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO 
    "abcdefghijklmnopqrstuvwxyz". 
    DISPLAY 'DATA AFTER CONVERTING : 'WS-DATA. 
    STOP RUN.  

Diagram -

Inspect converting workflow

The INSPECT validation performs in iterations and the number of iterations is exactly equal to the length of the data item. In the above example, the length of the data item is 10. So, the number of iterations is 10. The validation starts from 1st byte.

In the first iteration, the first character 'M' converts to its equal character 'm'. In the second iteration, the second character 'A' converts to its equal character 'a'. Similarly, every character converted with its equal characters. The result is 'mainframes'.