INSPECT Statement COBOL

The INSPECT statement provides the ability to tally (Format 1), replace (Format 2), or tally and replace (Format 3) occurrences of single characters or groups of characters in a data item.

Format 1:Inspect…Tallying

Inspect…Tallying

Format 2:Inspect…Replacing

Inspect…Replacing

Format 3:Inspect…Tallying…Replacing

Inspect…Tallying…Replacing

Format 4:Inspect…Converting

Inspect…Converting

identifier-1 must reference either a group item or any category of elementary items that have DISPLAY usage.

identifier-3, . . ., identifier-n must reference an elementary item that has DISPLAY usage.

Each literal must be a nonnumeric literal and may be any figurative constant except those that begin with the word ALL.If literal-1, literal-2 or literal-4 is a figurative constant, it refers to an implicit one-character data item.

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

For Formats 1 and 3:

  • identifier-2 must reference an elementary numeric data item.
  • For Formats 2 and 3:

  • The size of literal-3 or the data item referenced by identifier-5 must be equal to the size of literal-1 or the data item referenced by identifier-3.When a figurative constant is used as literal-3 , the size of the figurative constant is equal to the size of literal-1 or to the size of the data item referenced by identifier-3.
  • When the CHARACTERS phrase is used, literal-2, literal-3, or the size of the data item referenced by identifier-4, identifier-5 must be one character in length.
  • For Format 4:

  • he size of literal-5 or the data item referenced by identifier-7 must be equal to the size of literal-4 or the data item referenced by identifier-6.When a figurative constant is used as literal-5 , its size is equal to the size of literal-4 or to the size of the data item referenced by identifier-6.
  • The same character must not appear more than once either in literal-4 or in the data item referenced by identifier-6.

General Rules

The general rules that apply to the INSPECT statement are as follows:

  1. Inspection (which includes the comparison cycle, the establishment of boundaries for the BEFORE or AFTER phrase, and the mechanism for tallying, replacing, or both) begins at the leftmost character position of the data item referenced by identifier-1, regardless of its class, and proceeds from left to right to the rightmost character position as described in general rules 5 through 7.
  2. For use in the INSPECT statement, the contents of the data item referenced by identifier-1, identifier-3, identifier-4, identifier-5, identifier-6, or identifier-7 is treated as follows:
    • If any identifier-1, identifier-3, identifier-4 , identifier-5, identifier-6, or identifier-7 refers to an alphabetic or alphanumeric data item, the INSPECT statement treats the contents of each such data item as a character-string.
    • If any identifier-1, identifier-3, identifier-4, identifier-5, identifier-6 or identifier-7 refers to an alphanumeric edited, numeric edited or unsigned numeric data item, the data item is inspected as though it had been redefined as alphanumeric (see general rule 2a) and the INSPECT statement had been written to reference the redefined data item.
    • If any identifier-1, identifier-3, identifier-4, identifier-5, identifier-6 or identifier-7 refers to a signed numeric data item, the data item is inspected as though it had been moved to an unsigned numeric data item of the same length (excluding any separate sign position) and then the rules in general rule 2b had been applied. (See the discussion of the MOVE statement.)If identifier-1 is a signed numeric item, the original value of the sign is retained upon completion of the INSPECT statement.
  3. In general, rules 5 through 17, all references to literal-1, literal-2, literal-3, literal-4, and literal-5 apply equally to the contents of the data item referenced by identifier-3, identifier-4, identifier-5, identifier-6, and identifier-7, respectively.
  4. Subscripting associated with any identifier is evaluated only once as the first operation in the execution of the INSPECT statement.
  5. During inspection of the contents of the data item referenced by identifier-1, each properly matched occurrence of literal-1 is tallied (Formats 1 and 3) or replaced by literal-3 (Formats 2 and 3).
  6. The comparison operation to determine the occurrences of literal-1 to be tallied or to be replaced occurs as follows:
    • The operands of the TALLYING or REPLACING phrase are considered in the order they are specified in the INSPECT statement from left to right.The first literal-1 is compared to an equal number of contiguous characters, starting with the leftmost character position in the data item referenced by identifier-1. literal-1 matches that portion of the contents of the data item referenced by identifier-1 if they are equal, character for character, and if any of the following conditions are present:
      1. Neither LEADING nor FIRST is specified.
      2. The LEADING adjective applies to literal-1 and literal-1 is a leading occurrence as defined in general rules 10 and 13.
      3. The FIRST adjective applies to literal-1 and literal-1 is the first occurrence as defined in general rule 13.
    • If no match occurs in the comparison of the first literal- , the comparison is repeated with each successive literal-1, if any, until either a match is found or there is no next successive literal-1.In the latter case, the character position in the data item referenced by identifier-1 immediately to the right of the leftmost character position considered in the last comparison cycle is considered the new leftmost character position, and the comparison cycle begins again with the first literal-1.
    • Whenever a match occurs, tallying or replacing takes place as described in general rules 10 and 13.The character position in the data item referenced by identifier-1 immediately to the right of the rightmost character position that participated in the match is now considered to be the leftmost character position of the data item referenced by identifier-1, and the comparison cycle starts again with the first literal-1.
    • The comparison operation continues until the rightmost character position of the data item referenced by identifier-1 has participated in a match or has been considered as the leftmost character position.When this occurs, inspection is terminated.
    • If the CHARACTERS phrase is specified, an implied one-character operand participates in the cycle described in general rules 6a through 6d above as if it had been specified as literal-1 , except that no comparison to the contents of the data item referenced by identifier-1 takes place.This implied character is considered always to match the leftmost character of the contents of the data item referenced by identifier-1 participating in the current comparison cycle.
  7. The comparison operation defined in general rule 6 is affected by the BEFORE and AFTER phrases as follows:
    • If neither the BEFORE nor the AFTER phrase is specified, literal-1 or the implied operand of the CHARACTERS phrase participates in the comparison operation as described in general rule 6. literal-1 or the implied operand of the CHARACTERS phrase is first eligible to participate in matching at the leftmost character position of the data item referenced by identifier-1.
    • If the BEFORE phrase is specified, the associated literal-1 or the implied operand of the CHARACTERS phrase participates only in those comparison cycles which involve that portion of the contents of the data item referenced by identifier-1 from its leftmost character position up to, but not including, the first occurrence of literal-2 within the contents of the data item referenced by identifier-1.The position of this first occurrence is determined before the first cycle of the comparison operation described in general rule 6 is begun.If, on any comparison cycle, literal-1 or the implied operand of the CHARACTERS phrase is not eligible to participate, it is considered not to match the contents of the data item referenced by identifier-1.If there is no occurrence of literal-2 within the contents of the data item referenced by identifier-1, its associated literal-1 or the implied operand of the CHARACTERS phrase participates in the comparison operation as though the BEFORE phrase had not been specified.
    • If the AFTER phrase is specified, the associated literal-1 or the implied operand of the CHARACTERS phrase may participate only in those comparison cycles which involve that portion of the contents of the data item referenced by identifier-1 from the character position immediately to the right of the rightmost character position of the first occurrence of literal-2 within the contents of the data item referenced by id entifier-1 to the rightmost character position of the data item referenced by identifier-1 .This is the character position at which literal-1 or the implied operand of the CHARACTERS phrase is first eligible to participate in matching.The position of this first occurrence is determined before the first cycle of the comparison operation described in general rule 6 is begun.If, on any comparison cycle, literal-1 or the implied operand of the CHARACTERS phrase is not eligible to participate, it is considered not to match the contents of the data item referenced by identifier-1.If there is no occurrence of literal-2 within the contents of the data item referenced by identifier-1, its associated literal-1 or the implied operand of the CHARACTERS phrase is never eligible to participate in the comparison operation.
  8. For Format 1

  9. The required words ALL and LEADING are adjectives that apply to each succeeding literal-1 until the next adjective appears.
  10. The contents of the data item referenced by identifier-2 is not initialized before the execution of the INSPECT statement.
  11. The rules for tallying are as follows:
    • If the ALL phrase is specified, the contents of the data item referenced by identifier-2 are incremented by one for each occurrence of literal-1 matched within the contents of the data item referenced by identifier-1.
    • If the LEADING phrase is specified, the contents of the data item referenced by identifier-2 are incremented by one for the first and each subsequent contiguous occurrence of literal-1 matched within the contents of the data item referenced by identifier-1, provided that the leftmost such occurrence is at the point where comparison began in the first comparison cycle in which literal-1 was eligible to participate.
    • If the CHARACTERS phrase is specified, the contents of the data item referenced by identifier-2 are incremented by one for each character matched, in the sense of general rule 6e, within the contents of the data item referenced by identifier-1.
  12. If identifier-1, identifier-3 or identifier-4 occupies the same storage area as identifier-2 , the result of the execution of this statement is undefined, even if it is defined by the same data description entry.
  13. For Format 2:

  14. The required words ALL, LEADING and FIRST are adjectives that apply to each succeeding BY phrase until the next adjective appears.
  15. The rules for replacement are as follows:
    • When the CHARACTERS phrase is specified, each character matched, in the sense of general rule 6e, in the contents of the data item referenced by identifier-1 is replaced by literal-3.
    • When the adjective ALL is specified, each occurrence of literal-1 matched in the contents of the data item referenced by identifier-1 is replaced by literal-3.
    • When the adjective LEADING is specified, the first and each successive contiguous occurrence of literal-1 matched in the contents of the data item referenced by identifier-1 is replaced by literal-3, provided that the leftmost occurrence is at the point where comparison began in the first comparison cycle in which literal-1 was eligible to participate.
    • When the adjective FIRST is specified, the leftmost occurrence of literal-1 matched within the contents of the data item referenced by identifier-1 is replaced by literal-3.This rule applies to each successive specification of the FIRST phrase regardless of the value of literal-1.
  16. If identifier-3, identifier-4 or identifier-5 occupies the same storage area as identifier-1, the result of the execution of this statement is undefined, even if it is defined by the same data description entry.
  17. For Format 3:

  18. A Format 3 INSPECT statement is interpreted and executed as though two successive INSPECT statements specifying the same identifier-1 had been written with the first statement being a Format 1 statement with TALLYING phrases identical to those specified in the Format 3 statement, and the second statement being a Format 2 statement with REPLACING phrases identical to those specified in the Format 3 statement.The general rules given for matching and counting apply to the Format 1 statement and the general rules given for matching and replacing apply to the statement.If any of the identifiers in the Format 2 statement are Format 2, their subscripts are evaluated only once before executing the Format 1 Subscripted statement.
  19. For Format 4:

  20. A Format 4 INSPECT statement is interpreted and executed as though a Format 2 INSPECT statement specifying the same identifier-1 had been written with a series of ALL phrases, one for each character of literal-4.The effect is as if each of these ALL phrases referenced, as literal-1, a single character of literal-4 and referenced, as literal-3, the corresponding single character of literal-5.Correspondence between the characters of literal-4 and the characters of literal-5 is by ordinal position within the data item.
  21. If identifier-4, identifier-6, or identifier-7 occupies the same storage area as identifier-1 , the result of the execution of this statement is undefined, even if it is defined by the same data description entry.

INSPECT Statement Examples


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics