Summary -

In this topic, we described about the below sections -

INSPECT Converting converts all occurrences of a specific character(s) to user-supplied replacement character(s) in a data item.

INSPECT CONVERTING works on individual characters in the group of characters.

INSPECT CONVERTING
INSPECT ws-input 
	CONVERTING ws-source/source-literal
	TO ws-target/target-literal.

INSPECT ws-input 
	CONVERTING ws-source/source-literal
	TO ws-target/target-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.

CONVERTING phrase -

  • Converts all character(s) occurrences in a data item with user-provided replacement character(s).

ws-source or source-literal -

  • Specifies the character(s) to be replaced.
  • The same character should not appear more than once in total character(s).

ws-target or target-literal -

  • Specifies the replacing character(s).
  • The replacing character(s) should be the same size as the replaced character(s).

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 - Conveting all uppercase characters to lowercase.

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA CONVERTING 
              "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
			  "abcdefghijklmnopqrstuvwxyz". 
Result-       WS-DATA = "mainframes"

The below diagram explains how the INSPECT converts all uppercase characters to lowercase characters.

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'.

Scenario2 - Conveting all uppercase characters to lowercase before "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA CONVERTING 
              "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
			  "abcdefghijklmnopqrstuvwxyz" BEFORE "R". 
Result-       WS-DATA = "mainfRAMES"

In the above case, it converts all uppercase characters to lowercase characters before "R".

Scenario3 - Conveting all uppercase characters to lowercase after "F" The result is "mainfRAMES".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA CONVERTING 
              "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
			  "abcdefghijklmnopqrstuvwxyz" AFTER "F". 
Result-       WS-DATA = "MAINFrames"

In the above case, it converts all uppercase characters to lowercase characters after "F". The result is "MAINFrames".

Practical Example -

Scenario - Below example describes how the INSPECT CONVERTING statement used in COBOL programming.

Code -

INSPECT CONVERTING program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. INSPECTC.                                            
       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-SOURCE  PIC X(26) VALUE                                 
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ".                              
          05 WS-TARGET  PIC X(26) VALUE                                 
             "abcdefghijklmnopqrstuvwxyz".                              
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           INSPECT WS-DATA                                              
                   CONVERTING WS-SOURCE TO ws-target                    
                         BEFORE "IS".                                   
                                                                        
           DISPLAY "WS-DATA AFTER CONVERSION:     " WS-DATA.            
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

INSPECT CONVERTING program Output

Explaining Example -

In the above case, all the uppercase characters before "IS" is converted lowecase characters. The output is "cobol language IS A HIGH LEVEL LANGUAGE ".