Summary -

In this topic, we described about the below sections -

INSPECT REPLACING replaces all occurrence or some occurrences of chatacter(s) with given character(s) in the data item.

INSPECT REPLACING
INSPECT ws-input REPLACING 
	CHARACTERS BY ws-replacing-char/replacing-literal. 
 
INSPECT ws-input REPLACING 
	CHARACTERS BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].

INSPECT ws-input REPLACING 
	ALL ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal.

INSPECT ws-input REPLACING 
	ALL ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].
	
INSPECT ws-input REPLACING 
	LEADING ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal.

INSPECT ws-input REPLACING 
	LEADING ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].
	
INSPECT ws-input REPLACING 
	FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal.
	
INSPECT ws-input REPLACING 
	FIRST ws-replaced-char/replaced-literal 
	BY ws-replacing-char/replacing-literal 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].

ws-input -

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

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.

Examples -

Scenario1 - Replace all "-" with "/".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA              PIC X(10) VALUE "DD-MM-YYYY".
Code-         INSPECT WS-DATA REPLACING ALL "-" BY "/". 
Result-       WS-DATA = "DD/MM/YYYY"

The below diagram explains how the INSPECT replaces "-" with "/".

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 size 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 '-'. They are not equal, and the result is "False". So, 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 '-'. They are equal, and the result is 'True". So, the character gets replaced with '/'. Similarly, every character is compared, and is replaced with '/' when the result is "True".

Scenario2 - Replace characters with '$'.

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA REPLACING CHARACTERS BY "$".   
Result-       WS-DATA = "$$$$$$$$$$"

In the above case, every character is replaced by '$'. So the result is "$$$$$$$$$$".

Scenario3 - Replace characters with '$' before character "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA REPLACING CHARACTERS BY "$" BEFORE "R".   
Result-       WS-DATA = "$$$$$RAMES"

In the above case, every character before "R" is replaced by '$'. So the result is "$$$$$RAMES".

Scenario4 - Replace characters with '$' after character "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA REPLACING CHARACTERS BY "$" AFTER "R".   
Result-       WS-DATA = "MAINFR$$$$"

In the above case, every character after "R" is replaced by '$'. So the result is "MAINFR$$$$".

Scenario5 - Replace characters with '$' before first occurrence of "A".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA REPLACING CHARACTERS BY "$" BEFORE INITIAL "A".   
Result-       WS-DATA = "$AINFRAMES"

In the above case, every character before first occurrence of "A" is replaced by '$'. So the result is "$AINFRAMES".

Scenario6 - Replace characters with '$' after first occurrence of "A".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA REPLACING CHARACTERS BY "$" AFTER INITIAL "A".   
Result-       WS-DATA = "MA$$$$$$$$"

In the above case, every character after first occurrence of "A" is replaced by '$'. So the result is "MA$$$$$$$$".

Scenario7 - Replace all "A" characters with '$'.

Input-        WS-DATA = "MAINFRAMES IS LEGENDARY"
Declaration-  05 WS-DATA              PIC X(25) 
                         VALUE "MAINFRAMES IS LEGENDARY".
Code-         INSPECT WS-DATA REPLACING ALL "A" BY "$".   
Result-       WS-DATA = "M$INFR$MES IS LEGEND$RY"

In the above case, every character of "A" is replaced by '$'. So the result is "M$INFR$MES IS LEGEND$RY".

Scenario8 - Replace leading "MAIN" characters with '$$$$'.

Input-        WS-DATA = "MAINFRAMES IS LEGENDARY"
Declaration-  05 WS-DATA              PIC X(25) 
                         VALUE "MAINFRAMES IS LEGENDARY".
Code-         INSPECT WS-DATA REPLACING LEADING "MAIN" BY "$$$$".   
Result-       WS-DATA = "$$$$FRAMES IS LEGENDARY"

In the above case, characters of "MAIN" is replaced by '$$$$'. So the result is "$$$$FRAMES IS LEGENDARY".

Scenario9 - Replace first "LEGEND" characters with '$$$$$$'.

Input-        WS-DATA = "MAINFRAMES IS LEGENDARY"
Declaration-  05 WS-DATA              PIC X(25) 
                         VALUE "MAINFRAMES IS LEGENDARY".
Code-         INSPECT WS-DATA REPLACING FIRST "LEGEND" BY "$$$$$$".   
Result-       WS-DATA = "MAINFRAMES IS $$$$$$ARY"

In the above case, characters of "LEGEND" is replaced by '$$$$$$'. So the result is "MAINFRAMES IS $$$$$$ARY".

Scenario10 - Replace first "LANGUAGE" characters with '&&&&&&&&' after "IS".

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE"
Declaration-  05 WS-DATA              PIC X(40) 
                 VALUE "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".
Code-         INSPECT WS-DATA REPLACING FIRST "LANGUAGE" BY "&&&&&&&&" 
                      AFTER "IS".    
Result-       WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL &&&&&&&&"

In the above case, "LANGUAGE" is replaced by '&&&&&&&&' after "IS". So the result is "COBOL LANGUAGE IS A HIGH LEVEL &&&&&&&& ".

Scenario11 - Replace all spaces with zeroes.

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE"
Declaration-  05 WS-DATA              PIC X(50) 
                 VALUE "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".
Code-         INSPECT WS-DATA REPLACING ALL SPACES BY ZEROS.    
Result-       WS-DATA = "COBOL0LANGUAGE0IS0A0HIGH0LEVEL0LANGUAGE00000000000"

In the above case, WS-DATA has total 17 spaces and replaced by zeroes. So the result is "COBOL0LANGUAGE0IS0A0HIGH0LEVEL0LANGUAGE00000000000".

Scenario12 - Replace all spaces with zeroes before "LEVEL".

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE"
Declaration-  05 WS-DATA              PIC X(40) 
                 VALUE "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".
Code-         INSPECT WS-DATA REPLACING ALL SPACES BY ZEROS 
                      BEFORE "LEVEL".       
Result-       WS-DATA = "COBOL0LANGUAGE0IS0A0HIGH0LEVEL LANGUAGE "

In the above case, WS-DATA has total 5 spaces before "LEVEL" and those are replaced by zeroes. So the result is "COBOL0LANGUAGE0IS0A0HIGH0LEVEL LANGUAGE ".

Scenario13 - Replace all low-values with zeroes.

Input-        WS-DATA = LOW-VALUES
Declaration-  05 WS-DATA              PIC X(40). 
Code-         INSPECT WS-DATA REPLACING ALL LOW-VALUES BY ZEROS.       
Result-       WS-DATA = "0000000000000000000000000000000000000000"

In the above case, WS-DATA has LOW-VALUES and those are replaced by zeroes. So the result is "0000000000000000000000000000000000000000".

Scenario14 - Multiple Replacements (Replacing SPACES with "#", "LANGUAGE" with '&&&&&&&&' and "IS" with "**".

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE"
Declaration-  05 WS-DATA              PIC X(40) 
                 VALUE "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE". 
Code-         INSPECT WS-DATA REPLACING ALL SPACES BY "#"       
                          "LANGUAGE" BY "&&&&&&&&"
                          "IS" BY "**".           
Result-       WS-DATA = "COBOL#&&&&&&&&#**#A#HIGH#LEVEL#&&&&&&&&#"

In the above case, all SPACES replaced by "#", "LANGUAGE" by "&&&&&&&&" and "IS" by "**". So the result is "COBOL#&&&&&&&&#**#A#HIGH#LEVEL#&&&&&&&&#".

Practical Example -

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

Code -

INSPECT REPLACING program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. INSPECTR.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
                                                                        
       01 WS-VAR.                                                       
          05 WS-DATA    PIC X(50) VALUE                                 
             "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".                 
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           INSPECT WS-DATA REPLACING ALL SPACES BY "#" AFTER "LEVEL"    
                       "LANGUAGE" BY "&&&&&&&&" BEFORE "HIGH"           
                       "IS" BY "**".                                    
           DISPLAY "AFTER REPLACING:   " WS-DATA.                       
           STOP RUN.                                                      
********************** BOTTOM OF DATA ********************************

Output -

INSPECT REPLACING program Output

Explaining Example -

In the above case,

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

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