Compaq COBOL AAQ2G1FTK User Manual

Page 161

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements

5.2 Separating Data Using the UNSTRING Statement

PNTR contains the current position of the scanner in the sending item. The
second UNSTRING statement uses PNTR to begin scanning the additional
sending strings in FIELD1.

Because the compiler considers the leftmost character to be character position
1, the value of PNTR can be used to examine the next character. To do this,
describe the sending item as a table of characters and use PNTR as a sending
item subscript. This is shown in the following example:

01 FIELD1.

02 FIELD1-CHAR OCCURS 40 TIMES.
.
.
.
UNSTRING FIELD1

.
.
.
WITH POINTER PNTR.

IF FIELD1-CHAR(PNTR) = "X" ...

Another way to examine the next character of the sending item is to use the
UNSTRING statement to move the character to a 1-character receiving item:

UNSTRING FIELD1

.
.
.
WITH POINTER PNTR.

UNSTRING FIELD1 INTO CHAR1 WITH POINTER PNTR.
SUBTRACT 1 FROM PNTR.
IF CHAR1 = "X" ...

The program must decrement PNTR by 1 to work, because the second
UNSTRING statement increments the pointer by 1.

The program must initialize the POINTER phrase data item before the
UNSTRING statement uses it. The compiler will terminate the UNSTRING
operation if the initial value of the pointer is less than one or greater than the
length of the sending item. Such a pointer value causes an overflow condition.
Overflow conditions are discussed in Section 5.2.7.

5.2.6 Counting UNSTRING Receiving Items Using the TALLYING Phrase

The TALLYING phrase counts the number of receiving items that received data
from the sending item.

When an UNSTRING statement contains several receiving items, there are not
always as many sending strings as there are receiving items. The TALLYING
phrase provides a convenient method for keeping a count of how many receiving
items actually received strings. The following example shows how to use the
TALLYING phrase:

MOVE 0 TO RCOUNT.
UNSTRING FIELD1 DELIMITED BY ","

OR ALL SPACE
INTO FIELD2A

FIELD2B
FIELD2C
FIELD2D
FIELD2E
TALLYING IN RCOUNT.

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

Advertising