Compaq COBOL AAQ2G1FTK User Manual

Page 163

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements

5.2 Separating Data Using the UNSTRING Statement

Omitting the word INTO (or writing it as TO) before the receiving item list

Repeating the word INTO in the receiving item list as shown in this example:

UNSTRING FIELD1 DELIMITED BY SPACE

OR TAB
INTO FIELD2A DELIMITER IN DELIMA
INTO FIELD2B DELIMITER IN DELIMB
INTO FIELD2C DELIMITER IN DELIMC.

Writing the POINTER and TALLYING phrases in the wrong order (POINTER
must precede TALLYING)

Failing to understand the rules concerning subscript evaluation

5.3 Examining and Replacing Characters Using the INSPECT

Statement

The INSPECT statement examines the character positions in an item and counts
or replaces certain characters (or groups of characters) in that item.

Like the STRING and UNSTRING operations, INSPECT operations scan across
the item from left to right. Included in the INSPECT statement is an optional
phrase that allows scanning to begin or terminate upon detection of a delimiter
match. This feature allows scanning to begin within the item, as well as at the
leftmost position.

5.3.1 Using the TALLYING and REPLACING Options of the INSPECT Statement

The TALLYING operation, which counts certain characters in the item, and the
REPLACING operation, which replaces certain characters in the item, can be
applied either to the characters in the delimited area of the item being inspected,
or to only those characters that match a given character string or strings under
stated conditions. Consider the following sample statements, both of which cause
a scan of the complete item:

INSPECT FIELD1 TALLYING TLY FOR ALL "B".
INSPECT FIELD1 REPLACING ALL SPACE BY ZERO.

The first statement causes the compiler to scan FIELD1 looking for the character
B. Each time a B is found, TLY is incremented by 1.

The second statement causes the compiler to scan FIELD1 looking for spaces.
Each space found is replaced with a zero.

The TALLYING and REPLACING phrases support both single and multiple
arguments. For example, both of the following statements are valid:

INSPECT FIELD1 TALLYING TLY FOR ALL "A" "B" "C".
INSPECT FIELD1 REPLACING ALL "A" "B" "C" BY "D".

You can use both the TALLYING and REPLACING phrases in the same
INSPECT statement. However, when used together, the TALLYING phrase
must precede the REPLACING phrase. An INSPECT statement with both
phrases is equivalent to two separate INSPECT statements. In fact, the compiler
compiles such a statement into two distinct INSPECT statements. To simplify
debugging, write the two phrases in separate INSPECT statements.

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

Advertising