Compaq COBOL AAQ2G1FTK User Manual

Page 164

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements
5.3 Examining and Replacing Characters Using the INSPECT Statement

5.3.2 Restricting Data Inspection Using the BEFORE/AFTER Phrase

The BEFORE/AFTER phrase acts as a delimiter and can restrict the area of the
item being inspected.

The following sample statement counts only the zeros that precede the percent
sign ( % ) in FIELD1:

INSPECT FIELD1 TALLYING TLY

FOR ALL ZEROS BEFORE "%".

The delimiter (the percent sign in the preceding sample statement) can be a
single character, a string of characters, or any figurative constant. Furthermore,
it can be either an identifier or a literal.

If the delimiter is an identifier, it must be an elementary data item of
DISPLAY usage. It can be alphabetic, alphanumeric, or numeric, and it can
contain editing characters. The compiler always treats the item as if it had
been described as an alphanumeric string. It does this by implicit redefinition
of the item, as described in Section 5.3.3.

If the delimiter is a literal, it must be nonnumeric.

The compiler repeatedly compares the delimiter characters against an equal
number of characters in the item being inspected. If none of the characters
matches the delimiter, or if too few characters remain in the rightmost position
of the item for a full comparison, the compiler considers the comparison to be
unequal.

The examples of the INSPECT statement in Figure 5–2 illustrate the way the
delimiter character finds a match in the item being inspected. The underlined
characters indicate the portion of the item the statement inspects as a result of
the delimiters of the BEFORE and AFTER phrases. The remaining portion of the
item is ignored by the INSPECT statement.

The ellipses represent the position of the TALLYING or REPLACING phrase.
The compiler generates code that scans the item for a delimiter match before it
scans for the inspection operation (TALLYING or REPLACING), thus establishing
the limits of the operation before beginning the actual inspection. Section 5.3.4.1
further describes the separate scan.

5.3.3 Implicit Redefinition

The compiler requires that certain items referred to by the INSPECT statement
be alphanumeric items. If one of these items is described as another data class,
the compiler implicitly redefines that item so the INSPECT statement can handle
it as an alphanumeric string as follows:

If the item is alphabetic, alphanumeric edited, or unsigned numeric, the
item is redefined as alphanumeric. This is a compile-time operation; no data
movement occurs at run time.

If the item is signed numeric, the compiler generates code that first removes
the sign and then redefines the item as alphanumeric. If the sign is a
separate character, that character is ignored, essentially shortening the item,
and that character does not participate in the implicit redefinition. If the sign
is an overpunch on the leading or trailing digit, the sign value is removed and
the character is left with only the numeric value that was stored in it.

5–18 Using the STRING, UNSTRING, and INSPECT Statements

Advertising