Cobol and Mainframe

Inspect Clause in Cobol

Most of the time, you have a requirement to concatenate, split, replace or extract the characters from the string. In COBOL, you have various functions that can be used to manipulate the string as per your requirement. Let’s look at the INSPECT statement which is primarily used to count and replace the character in the field. We will discuss all of these clauses (Tallying, Replacing, and Converting) in the following discussion:

Syntax Details:

  • The Inspect clause with the Tallying clause is used to count specific characters.
  • The Inspect clause with the Replacing clause is used to replace specific characters.
  • The Inspect clause with the Tallying and Replacing clauses are used to count and replace characters.

The Aspect of Inspect Clause

  • To count the number of characters in a specific string, you can use INSPECT with the Tally option.
  • To replace the specific character of a string, you can use INSPECT with the Replacing clause.
  • You can use alphabetic, alphanumeric, or numeric data types but the numeric data type should be with usage as display. It should not be displayed.
  • The last point is you can use the variable, INSPECT clause with tallying, and replacing option to count and replace the character in a specific string.

Now, we will see some programming examples one by one.

Programming Example 1: INSPECT with REPLACE

IDENTIFICATION DIVISION.
PROGRAM-ID. FF.

DATA DIVISION.
FILE SECTION.


WORKING-STORAGE SECTION.

01 WS-DTE PIC X (10) VALUE "13-12-2022".
PROCEDURE DIVISION.

A000-MAIN-SECTION.

INSPECT WS-DTE REPLACING ALL "-" BY "/".
DISPLAY WS-DTE
STOP RUN.

Output:

papan@papan-VirtualBox: ~/Desktop /pp$ cobc -xjF mp.cbl
13/12/2022
papan@papan-VirtualBox: ~/Desktop /pp$

Explanation:
This is an example of COBOL INSPECT statement with the REPLACE option. What we did with it is we declared a variable called WS-DTE which is actually holding your date, which is “13-12-2022”. The procedure section and the procedure division have a paragraph called main section. There, you just use the INSPECT statement which is “INSPECT WS-DTE REPLACING “- “BY “/”. The output of this particular statement is by date which has “/” despite of.

Programming Example 2: INSPECT with TALLYING

IDENTIFICATION DIVISION.
PROGRAM-ID. BASIC.

DATA DIVISION.
FILE SECTION.


WORKING-STORAGE SECTION.

01 WS-NME PIC X(15) VALUE "SOMDEB NATH".
01 WS-CNT PIC 9(03) VALUE ZEROES.
PROCEDURE DIVISION.

A000-MAIN-SECTION.

INSPECT WS-NME TALLYING WS-CNT FOR ALL SPACES.
DISPLAY WS-CNT.
STOP RUN.

Output:

papan@ubuntu:~/Desktop/cobol$ cobc -xjF 1.cbl
005

Explanation:
In this example, we used two variables in working the storage section. The first variable is “WS-NME” which actually holds a name called “SOMDEB NATH”. The second variable is “WS-CNT” which stores the number of spaces in that particular string.

Now, let’s look at the procedure division and the section. It says INSERT WS-NME TALLYING WS-CNT FOR ALL SPACES. As an output, we get the count of spaces of that particular string.

Programming Example 3: INSPECT with REPLACE

IDENTIFICATION DIVISION.
PROGRAM-ID. MM.

DATA DIVISION.
FILE SECTION.


WORKING-STORAGE SECTION.

01 WS-NME PIC X(15) VALUE "DUDLEY DURSLEY".

PROCEDURE DIVISION.

A000-MAIN-SECTION.

INSPECT WS-NME REPLACING ALL "D" BY "d" AFTER INITIAL "D".
DISPLAY WS-NME.
STOP RUN.

Explanation:
This programming example is COBOL “INSPECT with REPLACE” statement. In this case, we use a replacing option in order to replace the letter “D” with its small letter “d”. If you see the “WS-NME” variable, it is actually has a name which is “DUDLEY DURSLEY”. In this case, we have three letter “D” in the “DUDLEY DURSLEY”. So, what it does is that it replaces the second letter because we use the replace option after the initial “D”. So, after the initial “D”, whatever letter “D” it encounters is replaced with “d”.

Programming Example 4: INSPECT with CONVERT

IDENTIFICATION DIVISION.
PROGRAM-ID. DD.

DATA DIVISION.
FILE SECTION.


WORKING-STORAGE SECTION.

01 WS-NME PIC X(15) VALUE "DUDLEY DURSLEY ".

PROCEDURE DIVISION.

A000-MAIN-SECTION.

INSPECT WS-NME CONVERTING
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
"abcdefghijklmnopqrstuvwxyz".
DISPLAY WS-NME.
STOP RUN.

Output:

papan@ubuntu:~/Desktop/cobol$ cobc -xjF 1.cbl
dudley dursley

Explanation:
The last but not the least programming example is about COBOL “INSPECT with CONVERT” statement. We convert the entire name into small letters. So, what we did is that using the COBOL INSPECT variable name “WS-NME” converting, we give all the alphabets in the code which is “A to Z”. Then, we specified all the corresponding small alphabets from “a” to “z”. The output is “Dudley dursley” with the second word in all lower case.

Conclusion

In this particular article, we covered all the aspects of “INSPECT” statement in the COBOL programming language, especially on the discussion of the “INSPECT” statement, its syntax details, and the application of this statement with regards to the program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com