COBOL INSPECT VERB USAGE & SYNTAX WITH EXAMPLES

INSPECT verb in COBOL is very useful and it is used to do the following functionalities

  •  Count a particular Character in a string
  •  Convert Lower case to upper case
  •  Replace a particular Character with other character in a string
  •  Replace a set of characters with another set of characters

Below are the different examples on different cases and syntax  which are very useful while coding a cobol program. Given the result for each case for better understanding.

Example 1: Capturing the count of any character in a string

It gives count of a specific character in a string. As in the below example, it counts all ‘-‘ in the date value string.

WA-CNT gives the result as 2

Example 2: Replacing one character with another one

This replaces all the ‘-‘ with ‘/’, so the result looks like 2011/07/20.

Example-3: Counting the exact length of a string using inspect

ww-in-strng2 pic x(10) value ‘REDD ‘, Here the actual length is 4 and remaining 6 chars are spaces. So here is one way to find the actual length using INSPECT.

Here it calculates all the spaces before it finds any character, in this case the wa-cnt2 would be 6. so the actual length would be length of(ww-in-strng2) – wa-cnt2 => 10 – 6 = 4

Ex4: Usage of Before and after

Here the resultant count is 3.

Counting for a specific character before any specific letter/character occurance.  In the example shown below, it is looking for letter E before the initial space. So it will not consider second part and checks the count in the first part (MAHENDER)  only for all ‘E’s.

but if the count of E is needed before any particular character say in this case SPACE, this can also be achieved like below.

Here Count is 2

Counting for a specific character after any other letter.  In the example shown below, it is looking for letter E after the initial space. So it will not consider  first part and checks the count in the second part (REDDY)  only for all ‘E’s.

Here Count is 1

Example 5: Usage of Before & After with Replacing

As we know that, we can replace any particular character with any others in string. But it also has a provision to replace a part of string that comes before/after any string. So it will not consider other part of the string. In the example shown below, it replaces only the Y which is placed after the character ‘@’ and before ‘.’.

So the result will be RXXYGGRXX@G.COM

Usage of FIRST:

First keyword in INSPECT is very useful while replacing any character with other one. In the below shown example, my requirement is to replace only the first R by G. It can be done as shown in below syntax.

Result: GGGGXWGYZ . Here it replaces all R’s by G.

Result: GRRRXWRYZ . Here it replaced only first R by G.

Usage of LEADING:

As opposed to FIRST keyword, Leading is used to replace all the first occurrences of R by G. As an example below it has a total of 5 R’s and in that 4 R’s are continuous with out any other character in between. So these first 4 would get replaced with G.

These Leading and FIRST can be used in combination with Initial, Before, After etc.

Result: GGGGXWRYZ . Here it replaced all the leading Rs by G.

Result: RRRRXWGYZ . As it is given before ‘Y’ and After ‘w’, It replaces only one R by G.

UPPER CASE TO LOWER CASE: This can be used to convert the upper case letters to lower case and vice versa but here the upper case and lower case letters needs to  be declared in working storage as shown below.

For further reading you can the official IBM portal @ https://www.ibm.com/support/knowledgecenter/en/SS6SG3_4.2.0/com.ibm.entcobol.doc_4.2/PGandLR/ref/rlpsinsp.htm

If you have any suggestions or concerns on this post, please feel to to post it via the comments below this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami