Compaq COBOL AAQ2G1FTK User Manual

Page 173

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements

5.3 Examining and Replacing Characters Using the INSPECT Statement

5.3.5.4 Interference in Tally Argument Lists

When several tally arguments contain one or more identical characters active
at the same time, they may interfere with each other, so that when one of the
arguments finds a match, the scanner steps past any other matching characters,
preventing those characters from being considered for a match.

The following two identical tally arguments do not interfere with each other
because they are not active at the same time. The first A in FIELD1 causes the
first argument to become inactive and the second argument to become active:

MOVE 0 TO T1 T2.
INSPECT FIELD1 TALLYING

T1 FOR ALL "," BEFORE "A"
T2 FOR ALL "," AFTER "A".

However, the next identical tally arguments interfere with each other since both
are active at the same time:

INSPECT FIELD1 TALLYING

T1 FOR ALL ","
T2 FOR ALL "," AFTER "A".

For any given position of the scanner, the arguments are applied to FIELD1
in the order in which they appear in the statement. When one of them finds
a match, the scanner moves to the next position and ignores the remaining
arguments in the argument list. Each comma in FIELD1 causes T1 to be
incremented by 1 and the second argument to be ignored. Thus, T1 always
contains an accurate count of all the commas in FIELD1, and T2 is always
unchanged.

The following INSPECT statement arguments only partially interfere with each
other:

INSPECT FIELD1 TALLYING

T2 FOR ALL "," AFTER "A"
T1 FOR ALL ",".

The first argument does not become active until the scanner encounters an
A. The second argument tallies all commas that precede the A. After the A,
the first argument counts all commas and causes the second argument to be
ignored. Thus, T1 contains the number of commas that precede the first A, and
T2 contains the number of commas that follow the first A. This statement works
well as written, but it could be difficult to debug.

The following three examples show that one INSPECT statement cannot count
any character more than once. Thus, when you use the same character in more
than one argument of an argument list, consider the possibility of interference
and choose the order of the arguments carefully. The solution may require two or
more INSPECT statements. Consider the following problem:

INSPECT FIELD1 TALLYING

T1 FOR ALL "AB"
T2 FOR ALL "BC".

If FIELD1 contains ABCABC after the scan, T1 is incremented by 2, and T2 is
unaltered. The successful matching of the argument includes each B in the item.
Each match resets the scanner to the character position to the right of the B, so
that the second argument is never successfully matched. The results remain the
same even if the order of the arguments is reversed. Only separate INSPECT
statements can develop the desired counts.

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

Advertising