Summary -

In this topic, we described about the below sections -

INSPECT TALLYING Counts the specific character(s) occurrences in a data item. The counter data type should be numeric.

INSPECT TALLYING
INSPECT ws-input TALLYING ws-count 
	FOR CHARACTERS.

INSPECT ws-input TALLYING ws-count 
	FOR CHARACTERS 
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].

INSPECT ws-input TALLYING ws-count
	FOR ALL ws-counting-chars/counting-literal.
	
INSPECT ws-input TALLYING ws-count
	FOR ALL ws-counting-chars/counting-literal
	[[BEFORE/AFTER] [INITIAL] ws-delimeter/delimeter-literal].

INSPECT ws-input TALLYING ws-count
	FOR LEADING ws-counting-chars/counting-literal.

INSPECT ws-input TALLYING ws-count
	FOR LEADING ws-counting-chars/counting-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.

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.

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 - Counting for ALL character "A".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "A". 
Result-       WS-CNT = 2

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

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 size of TALLYING character is one.

The first character is 'M' in the first iteration compared with TALLYING character 'A'. Both are not equal, and the result is False. So the counter (WS-CNT) doesn't increase. In the second iteration, the second character 'A' compared with TALLYING character 'A'. Both are equal, and the result is "True". So 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 resulting counter (WS-CNT) value is 2 at the end.

Scenario2 - Counting for characters.

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS.   
Result-       WS-CNT = 10

In the above case, WS-DATA has 10 characters. So the result is 10.

Scenario3 - Counting for characters before "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS 
                      BEFORE "R".   
Result-       WS-CNT = 05

In the above case, WS-DATA has 5 characters before "R". So the result is 5.

Scenario4 - Counting for characters after "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR CHARACTERS 
                      AFTER "R".  
Result-       WS-CNT = 4

In the above case, WS-DATA has 4 characters after "R". So the result is 4.

Scenario5 - Counting for LEADING "M".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR LEADING "M".  
Result-       WS-CNT = 1

In the above case, WS-DATA has 1 occurrence of leading "M". So the result is 1.

Scenario6 - Counting for all occurrence of "A" before "S".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "A"  
                      BEFORE "S".   
Result-       WS-CNT = 2

In the above case, WS-DATA has 2 occurrence of "A" before "S". So the result is 2.

Scenario7 - Counting for all occurrence of "A" after "I".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "A"  
                      AFTER "I".   
Result-       WS-CNT = 1

In the above case, WS-DATA has 1 occurrence of "A" after "I". So the result is 1.

Scenario8 - Counting for all occurrence of "FR".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL "FR".  
Result-       WS-CNT = 1

In the above case, WS-DATA has 1 occurrence of "FR". So the result is 1.

Scenario9 - Counting leading "M".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR LEADING "M".  
Result-       WS-CNT = 1

In the above case, WS-DATA has 1 occurrence of leading "M". So the result is 1.

Scenario10 - Counting leading "M" before "F".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA              PIC X(10) VALUE "MAINFRAMES".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR LEADING "M" 
                      BEFORE "F".  
Result-       WS-CNT = 1

In the above case, WS-DATA has 1 occurrence of leading "M" before "F". So the result is 1.

Scenario11 - Counting for all spaces.

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".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL SPACES. 
Result-       WS-CNT = 6

In the above case, WS-DATA has 6 spaces and 11 additional spaces. So the result is 17.

Scenario12 - Counting for all spaces before "LEVEL".

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".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL SPACES 
                      BEFORE "LEVEL".  
Result-       WS-CNT = 5

In the above case, WS-DATA has 5 spaces before word "ALL". So the result is 5.

Scenario13 - Counting for all zeroes.

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE. 102030405"
Declaration-  05 WS-DATA              PIC X(50) VALUE 
                     "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE. 102030405".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL ZEROES. 
Result-       WS-CNT = 6

In the above case, WS-DATA has 4 zeroes. So the result is 4.

Scenario14 - Counting for all zeroes before "4".

Input-        WS-DATA = "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE. 102030405"
Declaration-  05 WS-DATA              PIC X(50) VALUE 
                     "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE. 102030405".
              05 WS-CNT               PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT FOR ALL ZEROES 
                      BEFORE "4".  
Result-       WS-CNT = 3

In the above case, WS-DATA has 3 zeroes before "4". So the result is 3.

Scenario15 - Counting for all spaces, "A" and first "L" (Multiple Counting).

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.".
              05 WS-CNT1    PIC 9(02).
              05 WS-CNT2    PIC 9(02).
              05 WS-CNT3    PIC 9(02).
Code-         INSPECT WS-DATA TALLYING WS-CNT1 FOR ALL SPACES   
                         WS-CNT2 FOR ALL "A"      
                         WS-CNT3 FOR LEADING "L". 
Result-      WS-CNT1:     07
             WS-CNT2:     05
             WS-CNT3:     00

In the above case, WS-DATA has 7 spaces and 5 occurrences of "A" and no leading "L"s.


Practical Example -

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

Code -

INSPECT TALLYING program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. INSPECTS.                                            
       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".                             
           DISPLAY "WS-CNT1:     " WS-CNT1.                             
           DISPLAY "WS-CNT2:     " WS-CNT2.                             
           DISPLAY "WS-CNT3:     " WS-CNT3.                             
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

INSPECT TALLYING program Output

Explaining Example -

In the above 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.