Compaq COBOL AAQ2G1FTK User Manual

Page 149

Advertising
background image

Using the STRING, UNSTRING, and INSPECT Statements

5.1 Concatenating Data Using the STRING Statement

Figure 5–1 Results of the STRING Operation

16 spaces

AYER MA. 01432

ZK−6051−GE

A more attractive and readable report can be produced by having the STRING
operation produce this line:

AYER, MA. 01432

To accomplish this, use the figurative constant SPACE as a delimiter on the
sending item:

MOVE 1 TO P.
STRING CITY DELIMITED BY SPACE

INTO ADDRESS-LINE WITH POINTER P.

STRING ", " STATE ". " ZIP

DELIMITED BY SIZE
INTO ADDRESS-LINE WITH POINTER P.

This example makes use of the POINTER phrase (see Section 5.1.3). The first
STRING statement moves data characters until it encounters a space character—
a match of the delimiter SPACE. The second STRING statement supplies the
literal, the 2-character STATE item, another literal, and the 5-character ZIP item.

The delimiter can be varied for each item within a single STRING statement by
repeating the DELIMITED BY phrase after each of the sending item names to
which it applies. Thus, the shorter STRING statement in the following example
has the same effect as the two STRING statements in the preceding example.
(Placing the operands on separate source lines has no effect on the operation of
the statement, but it improves program readability and simplifies debugging.)

STRING CITY DELIMITED BY SPACE

", " STATE ". "
ZIP DELIMITED BY SIZE
INTO ADDRESS-LINE.

The sample STRING statement cannot handle 2-word city names, such as San
Francisco, because the compiler considers the space between the two words
as a match for the delimiter SPACE. A longer delimiter, such as two or three
spaces (nonnumeric literal), can solve this problem. Only when a sequence of
characters matches the delimiter does the movement stop for that data item.
With a 2-character delimiter, the same statement can be rewritten in a simpler
form:

STRING CITY ", " STATE ". " ZIP

DELIMITED BY "

" INTO ADDRESS-LINE.

Because only the CITY item contains two consecutive spaces, the delimiter’s
search of the other items will always be unsuccessful, and the effect is the same
as moving the full item (delimiting by SIZE).

Data movement under control of a data name or literal generally executes more
slowly than data movement delimited by SIZE.

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

Advertising