TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Inspect Statements


The INSPECT statement is used to perform various operations on string data. Each of the four formats works slightly differently and is described below,

  1. INSPECT TALLYING: This format is used to count occurrences of characters or strings within another string.

  2. INSPECT REPLACING: This format is used to replace occurrences of characters or strings within another string. The replacement strings must be of the same length as the original string.

  3. INSPECT TALLYING REPLACING: This format is used to count while replacing occurrences of characters or strings within another string. It is basically a combination of the first two formats.

  4. INSPECT CONVERTING: This format is used to replace occurrences of characters within another string using only one INSPECT.

    Any occurrence on the first character of the CONVERTING in the inspected string will be replaced by the first character in the TO string, while occurrences of the second character in the CONVERTING string will be replaced by the second character in the TO string, and so on. The CONVERTING string must be of the same length as the to string.
Let's see each format in detail below,

INSPECT TALLYING:

Tallying option is used to count the string characters.

Syntax:

INSPECT identifier-1 TALLYING identifier-2 FOR CHARACTERS.
INSPECT identifier-1 TALLYING identifier-2 FOR CHARACTERS [BEFORE/AFTER] [INITIAL] identifier-3 or literal-1.
INSPECT identifier-1 TALLYING identifier-2 FOR ALL [BEFORE/AFTER] [INITIAL] identifier-3 or literal-1.
INSPECT identifier-1 TALLYING identifier-2 FOR LEADINGS [BEFORE/AFTER] [INITIAL] identifier-3 or literal-1.

This phrase counts the occurrences of a specific character or special character in a data item.
  • identifier-1 is the inspected item and can be an alphanumeric group item or a national group item.

  • identifier-2 is the count field, and must be an elementary integer item.

  • identifier-3 or literal-1 is the tallying field (the item whose occurrences will be tallied).

  • CHARACTERS When CHARACTERS is specified and neither the BEFORE nor AFTER phrase is specified, the count field (identifier-2) is increased by 1 for each character (including the space character) in the inspected item (identifier-1). Thus, execution of an INSPECT statement with the TALLYING phrase increases the value in the count field by the number of character positions in the inspected item.

  • ALL When ALL is specified and neither the BEFORE nor AFTER phrase is specified, the count field (identifier-2) is increased by 1 for each nonoverlapping occurrence of the tallying comparand (identifier-3 or literal-1) in the inspected item (identifier-1), beginning at the leftmost character position and continuing to the rightmost.

  • LEADING When LEADING is specified and neither the BEFORE nor AFTER phrase is specified, the count field (identifier-2) is increased by 1 for each contiguous nonoverlapping occurrence of the tallying comparand in the inspected item (identifier-1), provided that the leftmost such occurrence is at the point where comparison began in the first comparison cycle for which the tallying comparand is eligible to participate.
Example:

01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
...........
      INSPECT WS-STRING TALLYING WS-CNT1 FOR CHARACTER.
      DISPLAY "WS-CNT1 : "WS-CNT1.

      INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL 'A'.
      DISPLAY "WS-CNT2 : "WS-CNT2.

Output:

WS-CNT1 : 15
WS-CNT2 : 06

INSPECT REPLACING:

This phrase fills all or portions of a data item with specified characters, such as spaces or zeros.

Syntax:

INSPECT identifier-1 REPLACING CHARACTERS BY identifier-5 or literal-3.
INSPECT identifier-1 REPLACING CHARACTERS BY identifier-5 or literal-3 [BEFORE/AFTER] [INITIAL] identifier-4 or literal-2.
INSPECT identifier-1 REPLACING ALL identifier-3 or literal-1 BY identifier-5 or literal-3.
INSPECT identifier-1 REPLACING ALL identifier-3 or literal-1 BY identifier-5 or literal-3 [BEFORE/AFTER] [INITIAL] identifier-4 or literal-2.
INSPECT identifier-1 REPLACING LEADING identifier-3 or literal-1 BY identifier-5 or literal-3.
INSPECT identifier-1 REPLACING LEADING identifier-3 or literal-1 BY identifier-5 or literal-3 [BEFORE/AFTER] [INITIAL] identifier-4 or literal-2.
INSPECT identifier-1 REPLACING FIRST identifier-3 or literal-1 BY identifier-5 or literal-3.
INSPECT identifier-1 REPLACING FIRST identifier-3 or literal-1 BY identifier-5 or literal-3 [BEFORE/AFTER] [INITIAL] identifier-4 or literal-2.

This phrase counts the occurrences of a specific character or special character in a data item.
  • identifier-1 is the inspected item and can be an alphanumeric group item or a national group item.

  • identifier-3 or literal-1 is the subject field, which identifies the characters to be replaced.

  • identifier-5 or literal-3 is the substitution field (the item that replaces the subject field). The subject field and the substitution field must be the same length.

  • CHARACTERS BY When the CHARACTERS BY phrase is used, the substitution field must be one character position in length. When CHARACTERS BY is specified and neither the BEFORE nor AFTER phrase is specified, the substitution field replaces each character in the inspected item (identifier-1), beginning at the leftmost character position and continuing to the rightmost.

  • ALL When ALL is specified and neither the BEFORE nor AFTER phrase is specified, the substitution field replaces each nonoverlapping occurrence of the subject field in the inspected item (identifier-1), beginning at the leftmost character position and continuing to the rightmost.

  • LEADING When LEADING is specified and neither the BEFORE nor AFTER phrase is specified, the substitution field replaces each contiguous nonoverlapping occurrence of the subject field in the inspected item (identifier-1), provided that the leftmost such occurrence is at the point where comparison began in the first comparison cycle for which this substitution field is eligible to participate.

  • FIRST When FIRST is specified and neither the BEFORE nor AFTER phrase is specified, the substitution field replaces the leftmost occurrence of the subject field in the inspected item (identifier-1).
Example:

In the following example, the INSPECT statement examines and replaces characters in data item DATA-3. Each character that precedes the first instance of a quotation mark (") is replaced by the character 0.

  77  COUNTR            PIC 9   VALUE ZERO.
  01  DATA-3            PIC X(8).
  . . .
      INSPECT DATA-3
        REPLACING CHARACTERS BY ZEROS BEFORE INITIAL QUOTE.

DATA-3 beforeCOUNTR afterDATA-3 after
456"ABEL0000"ABEL
ANDES"12000000"12
"TWAS BR0"TWAS BR

INSPECT TALLYING REPLACING:

Counts the occurrences of specific characters and fills all or portions of a data item with specified characters, such as spaces or zeros.

Syntax:

 Combination of TALLING and REPLACING syntax

Only difference is First
When FIRST is specified and neither the BEFORE nor AFTER phrase is specified, the substitution field replaces the leftmost occurrence of the subject field in the inspected item (identifier-1).

When both the TALLYING and REPLACING phrases are specified (format 3), the INSPECT statement is executed as if an INSPECT TALLYING statement (format 1) were specified, immediately followed by an INSPECT REPLACING statement (format 2).

The following replacement rules apply:
  1. When the subject field is a figurative constant, the one-character substitution field replaces each character in the inspected item that is equivalent to the figurative constant.

  2. When the substitution field is a figurative constant, the substitution field replaces each nonoverlapping occurrence of the subject field in the inspected item.

  3. When the subject and substitution fields are character-strings, the character-string specified in the substitution field replaces each nonoverlapping occurrence of the subject field in the inspected item.

  4. After replacement has occurred in a given character position in the inspected item, no further replacement for that character position is made in this execution of the INSPECT statement.
Example:

In the following example, the INSPECT statement examines and replaces characters in data item DATA-2. The number of times a leading zero (0) occurs in the data item is accumulated in COUNTR. The first instance of the character A that follows the first instance of the character C is replaced by the character 2.

77  COUNTR            PIC 9   VALUE ZERO.
01  DATA-2            PIC X(11).
. . .
    INSPECT DATA-2
      TALLYING COUNTR FOR LEADING "0"
      REPLACING FIRST "A" BY "2" AFTER INITIAL "C".

DATA-2 beforeCOUNTR afterDATA-2 after
00ACADEMY00200AC2DEMY00
0000ALABAMA40000ALABAMA
CHATHAM00000CH2THAM0000

INSPECT CONVERTING:

Converts all occurrences of a specific character or string of characters in a data item (identifier-1) to user-supplied replacement characters.

Syntax:

INSPECT identifier-1 CONVERTING identifier-6 or literal-4 TO identifier-7 or literal-5
INSPECT identifier-1 CONVERTING identifier-6 or literal-4 TO identifier-7 or literal-5 [BEFORE/AFTER] [INITIAL] identifier-4 or literal-2.

This phrase counts the occurrences of a specific character or special character in a data item.
  • identifier-1 is the inspected item and can be an alphanumeric group item or a national group item.

  • identifier-6 or literal-4 Specifies the character string to be replaced. The same character must not appear more than once in either literal-4 or identifier-6.

  • identifier-7 or literal-5 Specifies the replacing character string. The replacing character string (identifier-7 or literal-5) must be the same size as the replaced character string (identifier-6 or literal-4).
Example:

The following example shows the use of INSPECT CONVERTING with AFTER and BEFORE phrases to examine and replace characters in data item DATA-4. All characters that follow the first instance of the character / but that precede the first instance of the character ? (if any) are translated from lowercase to uppercase.

01  DATA-4            PIC X(11).
. . .
    INSPECT DATA-4
      CONVERTING
         "abcdefghijklmnopqrstuvwxyz" TO
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      AFTER INITIAL "/"
      BEFORE INITIAL"?"

DATA-4 beforeDATA-4 after
a/five/?sixa/FIVE/?six
r/Rexx/RRRrr/REXX/RRRR
zfour?inspezfour?inspe

BEFORE and AFTER phrases (all formats)

This phrase narrows the set of items being tallied or replaced.

No more than one BEFORE phrase and one AFTER phrase can be specified for any one ALL, LEADING, CHARACTERS, FIRST or CONVERTING phrase.

identifier-4 or literal-2 is the delimiter. Delimiters are not counted or replaced.

INITIAL The first occurrence of a specified item. The BEFORE and AFTER phrases change how counting and replacing are done:

When BEFORE is specified, counting or replacing of the inspected item (identifier-1) begins at the leftmost character position and continues until the first occurrence of the delimiter is encountered. If no delimiter is present in the inspected item, counting or replacing continues toward the rightmost character position.

When AFTER is specified, counting or replacing of the inspected item (identifier-1) begins with the first character position to the right of the delimiter and continues toward the rightmost character position in the inspected item. If no delimiter is present in the inspected item, no counting or replacement takes place.

Tips:
  • INSPECT REPLACING can be used to convert leading spaces to zeros, in a numeric item, e.g.

    INSPECT LEADING-SPACE-ITEM REPLACING LEADING SPACES BY ZEROS.

  • INSPECT CONVERTING can be used to convert lowercase to uppercase, e.g.

    INSPECT MIXED-CASE-ITEM CONVERTING "abcde..." TO "ABCDE...".


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal