Compaq COBOL AAQ2G1FTK User Manual

Page 148

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements
5.1 Concatenating Data Using the STRING Statement

If FIELD2 is not large enough to hold all three items, the operation stops when it
is full. If the operation stops while moving one of the sending items, the compiler
ignores the remaining characters of that item and any other sending items not
yet processed. For example, if FIELD2 is filled while it is receiving FIELD1B, the
compiler ignores the rest of FIELD1B and all of FIELD1C.

If the sending items do not fill the receiving item, the operation stops when the
last character of the last sending item (FIELD1C) is moved. It does not alter the
contents nor space-fill the remaining character positions of the receiving item.

The sending items can be nonnumeric literals and figurative constants (except for
ALL literal). Example 5–1 sets up an address label by stringing the data items
CITY, STATE, and ZIP into ADDRESS-LINE. The figurative constant SPACE and
the literal period ( . ) are used to separate the information.

Example 5–1 Using the STRING Statement and Literals

01 ADDRESS-GROUP.

03 CITY

PIC X(20).

03 STATE

PIC XX.

03 ZIP

PIC X(5).

01 ADDRESS-LINE

PIC X(31).

.
.
.

PROCEDURE DIVISION.
BEGIN.

STRING CITY SPACE STATE ". " SPACE ZIP

DELIMITED BY SIZE INTO ADDRESS-LINE.

.
.
.

5.1.2 Using the DELIMITED BY Phrase

Although the sending items of the STRING statement are fixed in size at
compile time, they are frequently filled with spaces. For example, if a 20-
character city item contains the text MAYNARD followed by 13 spaces, the
STRING statement using the DELIMITED BY SIZE phrase would move the text
(MAYNARD) and the unwanted 13 spaces (assuming the receiving item is at least
20 characters long). The DELIMITED BY phrase, written with a data name or
literal, eliminates this problem.

The delimiter can be a literal, a data item, a figurative constant, or the word
SIZE. It cannot, however, be ALL literal, since ALL literal has an indefinite
length. When the phrase contains the word SIZE, the compiler moves each
sending item in total, until it either exhausts the characters in the sending item
or fills the receiving item.

If you use the code in Example 5–1, and CITY is a 20-character item, the result
of the STRING operation might look like Figure 5–1.

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

Advertising