0% found this document useful (0 votes)
138 views4 pages

INSPECT With TALLYING Option

The INSPECT verb in COBOL allows counting, replacing, or converting characters in a string. It has options for TALLYING to count characters, REPLACING to substitute characters, and CONVERTING to map one set of characters to another on a one-to-one basis. INSPECT examples provided demonstrate how to count and replace characters at different positions in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views4 pages

INSPECT With TALLYING Option

The INSPECT verb in COBOL allows counting, replacing, or converting characters in a string. It has options for TALLYING to count characters, REPLACING to substitute characters, and CONVERTING to map one set of characters to another on a one-to-one basis. INSPECT examples provided demonstrate how to count and replace characters at different positions in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

INSPECT

INSPECT verb allows to count and/or replace a character or a group


of characters. INSPECT has options TALLYING, REPLACING & CONVERTING.

INSPECT … with TALLYING option

This form counts/tally a character or a group of characters in


source string.

INSPECT Examples:
Source-string = “AABAbbACABA”

1. INSPECT source-string TALLYING tally-counter FOR CHARACTERS


BEFORE INITIAL ‘C’

In this example tally counter will have count of all characters


before first occurrence of ‘C’ in source-string.
Tally-counter will be ‘7’. “AABAbbACABA”

2. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’


In this example tally counter will have count of all occurrences
of ‘A’ in source-string.

Tally-counter will be ‘6’. “AABAbbACABA”

3. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’


AFTER INITIAL ‘B’

In this example tally counter will have no of occurrences of ‘A’


after first occurrence of ‘B’.

Tally-counter will be ‘4’. “AABAbbACABA”

4. INSPECT source-string TALLYING tally-counter FOR LEADING ‘A’

In this example tally counter will have no of leading A’s.


Tally-counter will be ‘2’. “AABAbbACABA”

5. Source-string = " SSET"


If we need to get string without spaces, trim the string. we can
use
following logic.

INSPECT FUNCTION REVERSE (Source-string) TALLYING space-count FOR


LEADING SPACES.
COMPUTE length-of-string = 6 - space-count.
Move Source-string(space-count+1 : length-of-string ) TO ws-
target-string.

Above INSPECT command get the no of leading spaces


from the string. after executing the INSPECT command
space-count variable contains 2.

In compute statement, space-count subtracted from


length of Source-string. value 4 will be stored
in length-of-string.

In move statement, Using referece modification, moved


actual string to ws-target-string. removed spaces.

INSPECT… with REPLACING option

This form replaces a character or group of characters

Example:
Source-string = “AABAbbACABA”

1. INSPECT source-string REPLACING CHARACTERS BY ‘#’ BEFOR INITIAL


‘C’.

In above example all characters before first occurrence of ‘C’


in source-string are replaced by ‘#’.

Input : “AABAbbACABA”

Output: -“#######CABA”.

2. INSPECT source-string REPLACING ALL ‘A’ BY ‘#’

In above example all occurrences of ‘A’ in source-string are


replaced by ‘#’.

Input : “AABAbbACABA”

Output: - “##B#bb#C#B#”.

TIPS :
With CHARACTERS phrase only one character size of replacing
string should be specified. And also delimiter (‘C’) must be
of one character.

Replacing string and replaced string must be of same size if


we use phrases ALL, LEADING and FIRST.

INSPECT… with TALLYING and REPLACING options

This form counts/tally a character or group of characters and


replaces a character or group of characters.

Example:
Source-string = “AABAbbACAB”

1. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’ AFTER


INITIAL ‘B’
REPLACING ALL ‘A’ BY ‘#’ AFTER INITIAL ‘B’

In above example all occurrences of ‘A’ after first occurrence


of ‘B’ in source-string are counted and replaced by ‘#’.

Tally-counter will be ‘3’


Source-string will be “AAB#bb#C#B”.

INSPECT… with CONVERTING option

This form converts characters from one sequence string to characters


from equal size other sequence string on one to one mapping basis.

These two sequence strings must be of same size.

Example:

Source-string = “AABAbbACAB”

1. INSPECT source-string CONVERTING ‘BXCY’ TO ‘1234’

In above example converts the characters B, X, C, Y to 1, 2, 3, and 4


respectively.

Input : “AABAbbACAB”
Output: “AA1AbbA3A1”

Equivalent INSPECT with REPLACING option

INSPECT source-string REPLACING ALL ‘B’ BY ‘1’


ALL ‘X’ BY ‘2’
ALL ‘C’ BY ‘3’
ALL ‘Y’ BY ‘4’

Difference between CONVERTING and REPLACING options in INSPECT

Source-string = “AABAbbACAB”

INSPECT source-string CONVERTING ‘AB’ TO ‘12’


Output: - “1121bb1C12”

INSPECT source-string REPLACING ‘AB’ TO ‘12’


Output:- “A12AbbAC12”

In example 1, all occurrences of characters A and B are replaced by


characters
1 and 2 respectively.
In example2, all occurrences of string ‘AB’ is replaced by string
‘12’.

You might also like