COBOL Inspect Statement

COBOL Initialize COBOL Examine

Advertisements


INSPECT statement is used to do the following tasks by using its four formats.


  1. INSPECT TALLYING: Used to count the characters of a particular string.
  2. INSPECT REPLACING: Replaces single/group of characters by using single/group of characters in a particular string.
  3. INSPECT TALLYING REPLACING: Above two operations in one shot.
  4. INSPECT CONVERTING: Converts the each character in set of characters to corresponding characters in another set of characters.

Let’s discuss in detail about each one of the above INSPECT types.


INSPECT TALLYING:


INSPECT TALLYING used to count the characters of a particular string.


INSPECT TALLYING Syntax:



INSPECT TALLYING can be used to count the occurrence of single character as well as set of characters.

INSPECT TALLYING had no particular specification to the counter field declaration but it should be declared as Numeric.

INSPECT TALLYING consider character by character to verify whether matching with given character/set of characters.


INSPECT TALLYING work flow:

Let’s take an example to explain the flow.


Assume the below,

01 WS-DATA		PIC X(10) VALUE ‘MAINFRAMES’.
01 WS-CNT 	 	PIC 9(02) VALUE ZEROES.

INSPECT WS-DATA TALLYING WS-CNT FOR ALL ‘A’.

The below diagram can explain briefly about how the INSPECT will verify the data.


Diagram:



The INSPECT validation can be stated from first byte and length of validation is based on the TALLYING character(s).

The validation can be done in Iterations which are exactly the length of the data item.

In the above example, the length of the data item is 10. So the number of iterations will be 10. The validation starts from 1st byte and the length of TALLYING character is one. So the length of validation character in the data item was 1.

In the first iteration, It will take the first character “M” and compare with TALLYING character “A” which false. So the counter (WS-CNT) will not increase.

In the second iteration, It will take the second character “A” and compare with TALLYING character “A” which true. So the counter (WS-CNT) increased by 1.

Similarly, 10 iterations will be completed and the counter (WS-CNT) value will be 2 at the end.


INSPECT TALLYING example:


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.                                                   


INSPECT REPLACING:


INSPECT REPLACING replaces single/group of characters by using single/group of characters in a particular string.


INSPECT REPLACING Syntax:



INSPECT REPLACING can be used to replace the occurrence of single character as well as set of characters.

INSPECT REPLACING consider character by character to verify whether matching with given character/set of characters.


INSPECT REPLACING work flow:

Let’s take an example to explain the flow.


Assume the below,

01 WS-DATA		PIC X(10) VALUE ‘DD-MM-YYYY’.

	INSPECT WS-DATA REPLACING ALL ‘-‘ BY ‘/’.

Diagram:



The INSPECT validation can be stated from first byte and length of validation is based on the REPLACING character(s).

The validation can be done in Iterations which are exactly the length of the data item.

In the above example, the length of the data item is 10. So the number of iterations will be 10. The validation starts from 1st byte and the length of REPLACING character is one. So the length of validation character in the data item was 1.

In the first iteration, It will take the first character “M” and compare with validating character ‘”-“ If false, will move to the next iteration for validation next character.

In the second iteration, It will take the second character “M” and compare with validating character ‘”-“ If false, will move to the next iteration for validation next character.

In the third iteration, It will take the third character “-” and compare with validating character ‘”-“ If true, REPLACE with replacing character “/” and will move to the next iteration for validation next character.

Similarly, 10 iterations will be completed and replace all the matching characters with REPLACING character(s).


Example:


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.                                                   


INSPECT TALLYING REPLACING:


INSPECT TALLYING REPLACING used to count the characters of a particular string and replaces single/group of characters by using single/group of characters in a particular string.


INSPECT TALLYING REPLACING Syntax:



INSPECT TALLYING REPLACING can be used to count and replace the occurrence of single character as well as set of characters.

INSPECT TALLYING REPLACING considers character by character to verify whether matching with given character/set of characters.

INSPECT TALLYING REPLACING will perform the operations in the order like TALLYING first and REPLACING next.


INSPECT TALLYING REPLACING work flow:

Let’s take an example to explain the flow.


Assume the below,

01 WS-DATA		PIC X(10) VALUE ‘DD-MM-YYYY’.
01 WS-CNT 	 	PIC 9(02) VALUE ZEROES.

INSPECT WS-DATA TALLYING WS-CNT FOR ALL ‘-’
				REPLACING ALL ‘-‘ BY ‘/’.

Workflow Diagram:



The INSPECT validation can be stated from first byte and length of validation is based on the TALLYING/REPLACING character(s).

The validation can be done in Iterations which are exactly the length of the data item.

In the above example, the length of the data item is 10. So the number of iterations will be 10. The validation starts from 1st byte and the length of TALLYING/REPLACING character is one. So the length of validation character in the data item was 1.

In the first iteration, It will take the first character “M” and compare with validating character ‘”-“ If false, will move to the next iteration for validation next character.

In the second iteration, It will take the second character “M” and compare with validating character ‘”-“ If false, will move to the next iteration for validation next character.

In the third iteration, It will take the third character “-” and compare with validating character ‘”-“ If true, REPLACE with replacing character “/” and will move to the next iteration for validation next character. So the counter (WS-CNT) increased by 1.

Similarly, 10 iterations will be completed, replace all the matching characters with REPLACING character(s) and the counter (WS-CNT) value will be 2 at the end.


INSPECT TALLYING REPLACING example:


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.                                                   


INSPECT CONVERTING:


INSPECT CONVERTING Converts the each character in set of characters to corresponding characters in another set of characters.


INSPECT CONVERTING Syntax:



INSPECT CONVERTING will work on individual characters in the group of characters.


INSPECT CONVERTING work flow:

01 WS-DATA		PIC X(10) VALUE ‘MAINFRAMES’.


	INSPECT WS-DATA 
		CONVERTING
        		"abcdefghijklmnopqrstuvwxyz" TO
       	 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ".

Equal INSPECT replace is 

	INSPECT WS-DATA 
		REPLACING 	ALL ‘A‘ BY ‘a’,
        				ALL ‘B‘ BY ‘b’,
       					ALL ‘C‘ BY ‘c’,
						.
						.
						.
        				ALL ‘Z‘ BY ‘z’.

Workflow Diagram:



The INSPECT validation can be stated from first byte and length of validation is based on the CONVERTING character(s).

The validation can be done in Iterations which are exactly the length of the data item.

In the above example, the length of the data item is 10. So the number of iterations will be 10. The validation starts from 1st byte and the length of CONVERTING character is one(INSPECT CONVERTION will take single byte conversion). So the length of validation character in the data item was 1.

In the first iteration, It will take the first character “M” and CONVERT it to the equaling character ”m“ and move to the next iteration for CONVERTING next character.

In the second iteration, It will take the second character “I” and CONVERT it to the equaling character ”i“ and move to the next iteration for CONVERTING next character.

Similarly, 10 iterations will be completed, convert all the characters with corresponding character(s).


INSPECT CONVERTING example:


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.                                                   


COBOL Initialize COBOL Examine

Advertisements