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.
WW-IN-STRNG is 2011-07-20
TALLYING WA-CNT FOR ALL '-'
WA-CNT gives the result as 2
Example 2: Replacing one character with another one
REPLACING ALL '-' BY '/'
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.
INSPECT FUNCTION REVERSE (ww-in-strng2)
TALLYING WA-CNT2 FOR LEADING SPACES
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
WS-IN-STRNG3 is MAHENDER REDDY
TALLYING WT-CNT FOR ALL 'E'
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.
TALLYING WT-CNT FOR ALL 'E' BEFORE INITIAL SPACE
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.
WU-IN-STRN3 is MAHENDER REDDY
TALLYING WA-CNT FOR ALL 'E' AFTER INITIAL SPACE
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 ‘.’.
WR-IN-STRNG4 is 'RXXYGGRXX@Y.COM'
REPLACING ALL 'Y' BY 'G' AFTER INITIAL '@' BEFORE INITIAL '.'
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.
WK-IN-STRNG5 is 'RRRRXWRYZ'
REPLACING ALL 'R' BY 'G'
Result: GGGGXWGYZ . Here it replaces all R’s by G.
WR-IN-STRN is 'RRRRXWRYZ'
REPLACING FIRST 'R' 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.
REPLACING LEADING 'R' BY 'G'
Result: GGGGXWRYZ . Here it replaced all the leading Rs by G.
REPLACING LEADING 'R' BY 'G'
BEFORE INITIAL 'Y AFTER INITIAL 'W'
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.
05 wp-upper-case pic x(26) value "ABCD ... Z"
05 WP-LOWER-CASE PIC X(26) VALUE ' abcd... z'
converting WW-LOWER-CASE to ww-upper-case
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.