Summary -

In this topic, we described about the below sections -

INSPECT TALLYING REPLACING statement counts the specific character(s) occurrences and replaces all or some occurrences with specified character(s) in the data item.

INSPECT TALLYING REPLACING is used to count and replace the occurrence of character(s).

INSPECT TALLYING REPLACING performs the TALLYING operation first and REPLACING next.

INSPECT TALLYING REPLACING
INSPECT ws-input 
	TALLYING ws-count FOR CHARACTERS
	REPLACING CHARACTERS BY ws-replacing-char/replacing-literal. 

INSPECT ws-input 
	TALLYING ws-count FOR CHARACTERS
	[[BEFORE/AFTER] [INITIAL] ws-delimeter1/delimeter-literal1]
	REPLACING CHARACTERS BY ws-replacing-char/replacing-literal.

INSPECT ws-input 
	TALLYING ws-count FOR CHARACTERS
	REPLACING CHARACTERS BY ws-replacing-char/replacing-literal
	[[BEFORE/AFTER] [INITIAL] ws-delimeter2/delimeter-literal2].

INSPECT ws-input 
	TALLYING ws-count FOR CHARACTERS
	[[BEFORE/AFTER] [INITIAL] ws-delimeter1/delimeter-literal1]
	REPLACING CHARACTERS BY ws-replacing-char/replacing-literal
	[[BEFORE/AFTER] [INITIAL] ws-delimeter2/delimeter-literal2].
	
INSPECT ws-input 
	TALLYING ws-count
	FOR ALL/LEADING ws-counting-chars/counting-literal
	REPLACING ALL/LEADING/FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal. 

INSPECT ws-input 
	TALLYING ws-count
	FOR ALL/LEADING ws-counting-chars/counting-literal
	[[BEFORE/AFTER] [INITIAL] ws-delimeter1/delimeter-literal1]
	REPLACING ALL/LEADING/FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal.

INSPECT ws-input 
	TALLYING ws-count
	FOR ALL/LEADING ws-counting-chars/counting-literal
	REPLACING ALL/LEADING/FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter2/delimeter-literal2].
	
INSPECT ws-input 
	TALLYING ws-count
	FOR ALL/LEADING ws-counting-chars/counting-literal
	[[BEFORE/AFTER] [INITIAL] ws-delimeter1/delimeter-literal1]
	REPLACING ALL/LEADING/FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter2/delimeter-literal2].

ws-input -

  • Input/inspected variable or data item.
  • It should be an alphanumeric group item or an elementary data item declared with usage DISPLAY.

TALLYING phrase -

  • TALLYING phrase counts the occurrences of a specific character(s) in a data item.
  • It counts alphanumeric characters (bytes) in a data item.

ws-count -

  • It should be an elementary numeric data item without a decimal point.
  • It should initialize before using in INSPECT statement.

CHARACTERS -

  • The counter is increased by 1 for each character (includes space) in the inspected item when CHARACTERS alone specified (without BEFORE or AFTER phrases).
  • Suppose the BEFORE or AFTER phrase is specified along with CHARACTERS. In that case, the counter is increased by 1 depending on BEFORE or AFTER phrase.

ALL -

  • The counter is increased by 1 for each non-overlapping occurrence of tallying character(s) in the inspected item when ALL alone specified (without BEFORE or AFTER phrases).
  • The tallying starts at the leftmost character position and continuing to the rightmost.
  • Suppose the BEFORE or AFTER phrase is specified along with ALL. In that case, the counter is increased by 1 depending on BEFORE or AFTER phrase.

LEADING -

  • The counter is increased by 1 for each contiguous non-overlapping occurrence of the tallying character(s) in the inspected item when ALL alone specified (without BEFORE or AFTER phrases).
  • The tallying starts after the first matching occurrence.
  • Suppose the BEFORE or AFTER phrase is specified along with CHARACTERS. In that case, the counter is increased by 1 depending on BEFORE or AFTER phrase.

ws-counting-chars or counting-literal -

  • It is tallying field with tallying character(s).

ws-counting-chars -

  • It should be an elementary data item declared with usage DISPLAY.

counting-literal -

  • These should be of alphanumeric type.
  • When identifier-1 is of usage DISPLAY, literals must be of category alphanumeric.
  • When identifier-1 is of usage DISPLAY, literals can be any figurative constant that should not begin with the word "ALL" word.
  • All identifiers (except identifier-2) should declare with the same usage as identifier-1.
  • All literals should have category alphanumeric when identifier-1 declared with usage DISPLAY.

REPLACING Phase -

  • TALLYING phrase replaces the occurrences of a specific character(s) in a data item.

CHARACTERS BY -

  • If specified, the substitution field should be one character in length.
  • Each character is replaced by a substitution character (includes space) in the inspected item when CHARACTERS BY alone specified (without BEFORE or AFTER phrases).
  • Suppose the BEFORE or AFTER phrase is specified along with CHARACTERS BY. In that case, the character is replaced by a substitution character depending on BEFORE or AFTER phrase.

ALL -

  • Replaces all occurrences of identifier-3/literal-1 with identifier-5/literal-3.
  • Each occurrence is replaced by substitution character(s) when ALL alone specified (without BEFORE or AFTER phrases).
  • Suppose the BEFORE or AFTER phrase is specified along with ALL. In that case, the occurrence is replaced by substitution character(s) depending on BEFORE or AFTER phrase.

LEADING -

  • Replacing takes place at the point where the comparison began in the leftmost position. i.e., starting of the word.
  • Replaces leftmost subject character(s) by substitution character(s) when LEADING alone specified (without BEFORE or AFTER phrases).
  • Suppose the BEFORE or AFTER phrase is specified along with LEADING. In that case, the occurrence is replaced by substitution character(s) depending on BEFORE or AFTER phrase.

FIRST -

  • Replacing takes place at the point where the comparison began in the leftmost starting position. i.e., beginning string.
  • Replaces leftmost first subject character(s) by substitution character(s) when LEADING alone specified (without BEFORE or AFTER phrases).
  • Suppose the BEFORE or AFTER phrase is specified along with LEADING. In that case, the occurrence is replaced by substitution character(s) depending on BEFORE or AFTER phrase.

ws-replaced-char, ws-delimeter, ws-replacing-char -

  • It should be an elementary data item declared with usage DISPLAY.

replaced-literal, delimeter-literal, replacing-literal -

  • These should be of alphanumeric type.
  • When identifier-1 is of usage DISPLAY, literals must be of category alphanumeric.
  • When identifier-1 is of usage DISPLAY, literals can be any figurative constant that should not begin with the word "ALL" word.
  • All identifiers (except identifier-2) should declare with the same usage as identifier-1.
  • All literals should have category alphanumeric when identifier-1 declared with usage DISPLAY.

ws-replaced-char or replaced-literal -

  • It should be an elementary data item declared with usage DISPLAY.
  • Identifies the character(s) to be replaced.
  • Should have the same length of identifier-5 or literal-3.
  • Known as the subject field.

ws-replacing-char or replacing-literal -

  • It should be an elementary data item declared with usage DISPLAY.
  • Identifies the character(s) that replaces.
  • Should have the same length of identifier-3 or literal-1.
  • Known as substitution field.

BEFORE and AFTER phrases -

  • This phrase decides the inspecting position in the inspecting string. i.e., tallied or replaced position.
  • Only one BEFORE phrase and one AFTER phrase should specify for ALL, LEADING, CHARACTERS, FIRST, or CONVERTING phrase.
  • When BEFORE is specified, counting or replacing begins at the leftmost character position and continues till the first occurrence of the delimiter. In case no delimiter is found, counting or replacing continues until the last character of the total string.
  • When AFTER is specified, counting or replacing begins at the first character position to the right of the delimiter and continues until the last character of the total string. In case no delimiter is found, no counting or replacement takes place.

INITIAL -

  • It represents the first occurrence of a specified item.

ws-delimeter or delimeter-literal -

  • It acts as a delimiter.
  • It doesn't counted or replaced.

ws-delimeter -

  • It should be an elementary data item declared with usage DISPLAY.

delimeter-literal -

  • These should be of alphanumeric type.
  • When identifier-1 is of usage DISPLAY, literals must be of category alphanumeric.
  • When identifier-1 is of usage DISPLAY, literals can be any figurative constant that should not begin with the word "ALL" word.
  • All identifiers (except identifier-2) should declare with the same usage as identifier-1.
  • All literals should have category alphanumeric when identifier-1 declared with usage DISPLAY.

Examples -

Scenario1 - Count for all "-" and Replace all "-" with "/".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-" 
              REPLACING ALL "-" BY "/". 
Result-       WS-CNT = 2
              WS-DATA = "DD/MM/YYYY"

The below diagram explains how the INSPECT counts the number of occurrences of '-' and replaces with '/'.

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 the 1st byte, and the length of TALLYING and REPLACING characters is one.

In the first iteration, the first character 'D' compared with TALLYING character '-'. They are not equal, and the result is "False". The counter (WS-CNT) doesn't increase. Coming to REPLACING phase, the first character 'D' compared with REPLACING character '-'. They are not equal, and the result is "False". The character won't get replaced.

In the second iteration, it has the same process as above.

In the third iteration, the third character '-' compared with TALLYING character '-'. They are equal, and the result is true. The counter (WS-CNT) gets increased by 1. Coming to the REPLACING phase, the third character '-' compared with REPLACING character '-'. They are equal, and the result is "True". The character gets replaced with '/'.

Similarly, every character is compared with TALLYING and REPLACING character(s). i.e., the counter (WS-CNT) gets increased when the match is found and replaced with '/' when replacing the match found.

Scenario2 - Count for no of characters and Replace them with "&".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS
              REPLACING CHARACTERS BY "&". 
Result-       WS-CNT = 10
              WS-DATA = "&&&&&&&&&&"

In the above case, WS-DATA has 10 characters. So the count result is 10 and replaces all characters with "&". The result is "&&&&&&&&&&".

Scenario3 - Count for no of characters before "Y" and Replace them with "&".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS BEFORE "Y"
              REPLACING CHARACTERS BY "&". 
Result-       WS-CNT = 6
              WS-DATA = "&&&&&&&&&&"

In the above case, WS-DATA has 6 characters before "Y". So the count result is 6 and replaces all characters with "&". The result is "&&&&&&&&&&".

Scenario4 - Count for no of characters and Replace them with "&" before "Y".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS
              REPLACING CHARACTERS BY "&" BEFORE "Y". 
Result-       WS-CNT = 10
              WS-DATA = "&&&&&&YYYY"

In the above case, WS-DATA has 10 characters. So the count result is 10 and replaces all characters before "Y" with "&". The result is "&&&&&&YYYY".

Scenario5 - Count for no of characters before "Y" and Replace them with "&" before "Y".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS
              REPLACING CHARACTERS BY "&" BEFORE "Y". 
Result-       WS-CNT = 6
              WS-DATA = "&&&&&&YYYY"

In the above case, WS-DATA has 6 characters before "Y". So the count result is 6 and replaces all characters before "Y" with "&". The result is "&&&&&&YYYY".

Scenario6 - Count for all "-" before "M" and Replace all "-" with "/".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-" BEFORE "M"
              REPLACING ALL "-" BY "/". 
Result-       WS-CNT = 1
              WS-DATA = "DD/MM/YYYY"

In the above case, WS-DATA has 1 occurance of "-" before "M". So the count result is 1. However, it has no BEFORE or AFTER Phase for REPLACING. So replaces all occurrences of "-" with "/". The result is "DD/MM/YYYY".

Scenario7 - Count for all "-" and Replace all "-" with "/" before "M".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-" 
              REPLACING ALL "-" BY "/" BEFORE "M". 
Result-       WS-CNT = 2
              WS-DATA = "DD/MM-YYYY"

In the above case, WS-DATA has 2 occurance of "-". So the count result is 2. However, WS-DATA has one occurrence of "-" before "M". So only one occurrence replaced with "/". The result is "DD/MM-YYYY".

Scenario8 - Count for all "-" before "M" and Replace all "-" with "/" before "M".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-" BEFORE "M"
              REPLACING ALL "-" BY "/" BEFORE "M". 
Result-       WS-CNT = 1
              WS-DATA = "DD/MM-YYYY"

In the above case, WS-DATA has one occurrence of "-" before "M". The count result is 1. However, WS-DATA has one occurrence of "-" before "M". So only one occurrence is replaced with "/". The result is "DD/MM-YYYY".

Scenario9 - Count leading "-" before "M" and Replace all "-" with "/" before "M".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR LEADING "-" BEFORE "M"
              REPLACING ALL "-" BY "/". 
Result-       WS-CNT = 0
              WS-DATA = "DD/MM/YYYY"

In the above case, WS-DATA has no leading "-" before "M". The count result is 0 and replace all "-" with "/". The result is "DD/MM/YYYY".

Scenario10 - Count all "-" and Replace first "-" with "/".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
              05 WS-CNT               PIC 9(02) VALUE ZEROES. 
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "-"
              REPLACING FIRST "-" BY "/". 
Result-       WS-CNT = 2
              WS-DATA = "DD/MM-YYYY"

In the above case, WS-DATA has 2 occurrences of "-". The count result is 2 and replace the first "-" with "/". The result is "DD/MM/YYYY".

Practical Example -

Scenario - Below example describes how the INSPECT TALLYING REPLACING statement used for multiple conditions in COBOL programming.

Code -

INSPECT TALLYING REPLACING program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. INSPCTTR.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
                                                                        
       01 WS-VAR.                                                       
          05 WS-DATA    PIC X(40) VALUE                                 
             "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".                 
          05 WS-CNT1    PIC 9(02).                                      
          05 WS-CNT2    PIC 9(02).                                      
          05 WS-CNT3    PIC 9(02).                                      
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           INSPECT WS-DATA TALLYING                                     
                   WS-CNT1 FOR ALL SPACES BEFORE "HIGH"                 
                   WS-CNT2 FOR ALL "A" AFTER "IS"                       
                   WS-CNT3 FOR LEADING "C"                              
                   REPLACING ALL SPACES BY "#" AFTER "LEVEL"            
                             "LANGUAGE" BY "&&&&&&&&" BEFORE "HIGH"     
                             "IS" BY "**".                              
                                                                        
           DISPLAY "WS-CNT1:     " WS-CNT1.                             
           DISPLAY "WS-CNT2:     " WS-CNT2.                             
           DISPLAY "WS-CNT3:     " WS-CNT3.                             
                                                                        
           DISPLAY "AFTER REPLACING:   " WS-DATA.                       
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

INSPECT TALLYING REPLACING program Output

Explaining Example -

In the above counting case,

  • SPACES before "HIGH" has 4 occurrences.
  • "A" has 3 occurrences before "IS".
  • "C" has only one occurrence of leading.

After counting, the results are WS-CNT1 = 04, WS-CNT2 = 03 and WS-CNT3 = 01.

In the above replacing case,

  • SPACES AFTER "LEVEL" replaced by "#".
  • "LANGUAGE" before "HIGH" replaced by "&&&&&&&&".
  • "IS" replaced by "**".

After replacements, the result is "COBOL &&&&&&&& ** A HIGH LEVEL#LANGUAGE###########".