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
Содержание COBOL AAQ2G1FTK
Страница 22: ......
Страница 30: ......
Страница 94: ......
Страница 110: ......
Страница 146: ......
Страница 180: ......
Страница 194: ...Processing Files and Records 6 1 Defi...
Страница 300: ......
Страница 490: ......
Страница 516: ......
Страница 517: ......
Страница 530: ......
Страница 534: ......
Страница 590: ......
Страница 620: ......