Compaq COBOL AAQ2G1FTK User Manual

Page 174

Advertising
background image

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

Sometimes you can use the interference characteristics of the INSPECT
statement to your advantage. Consider the following sample argument list:

MOVE 0 TO T4 T3 T2 T1.
INSPECT FIELD1 TALLYING

T4 FOR ALL "****"
T3 FOR ALL "***"
T2 FOR ALL "**"
T1 FOR ALL "*".

The argument list counts all of the asterisks in FIELD1 in four different tally
counters. T4 counts the number of times that four asterisks occur together; T3
counts the number of times three asterisks appear together; T2 counts double
asterisks; and T1 counts singles.

If FIELD1 contains a string of more than four consecutive asterisks, the
argument list breaks the string into groups of four and counts them in T4. It
then counts the less-than-four remainder in T3, T2, or T1.

Reversing the order of the arguments in this list causes T1 to count all of the
asterisks, and T2, T3, and T4 to remain unchanged.

When the LEADING condition is used with an argument in the argument list,
that argument becomes inactive as soon as it fails to be matched in the item
being inspected. Therefore, when two arguments in an argument list contain one
or more identical characters and one of the arguments has a LEADING condition,
the argument with the LEADING condition should appear first. Consider the
following sample statement:

MOVE 0 TO T1 T2.
INSPECT FIELD1 TALLYING

T1 FOR LEADING "*"
T2 FOR ALL "*".

T1 counts only leading asterisks in FIELD1; the occurrence of any other character
causes the first tally argument to become inactive. T2 keeps a count of any
remaining asterisks in FIELD1.

Reversing the order of the arguments in the following statement results in an
argument list that can never increment T1:

INSPECT FIELD1 TALLYING

T2 FOR ALL "*"
T1 FOR LEADING "*".

If the first character in FIELD1 is not an asterisk, neither argument can match
it, and the second argument becomes inactive. If the first character in FIELD1
is an asterisk, the first argument matches it and causes the second argument to
be ignored. The first character in FIELD1 that is not an asterisk fails to match
the first argument, and the second argument becomes inactive because it has not
found a match in any of the preceding characters.

An argument with both a LEADING condition and a BEFORE phrase can
sometimes successfully delimit the item being inspected, as in the following
example:

MOVE 0 TO T1 T2.
INSPECT FIELD1 TALLYING

T1 FOR LEADING SPACES
T2 FOR ALL "

" BEFORE "."

T2 FOR ALL "

" BEFORE "."

T2 FOR ALL " " BEFORE ".".

IF T2 > 0 ADD 1 TO T2.

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

Advertising