[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
The STRING, UNSTRING, and INSPECT statements give your HP COBOL programs the following capabilities:
The STRING statement concatenates the contents of one or more sending items into a single receiving item.
The statement has many forms; the simplest is equivalent in function to a nonnumeric MOVE statement. Consider the following example:
STRING FIELD1 DELIMITED BY SIZE INTO FIELD2. |
If the two items are the same size, or if the sending item (FIELD1) is larger, the statement is equivalent to the following statement:
MOVE FIELD1 TO FIELD2. |
If the sending item of the string is shorter than the receiving item, the compiler does not replace unused positions in the receiving item with spaces. Thus, the STRING statement can leave some portion of the receiving item unchanged.
The receiving item of the string must be an elementary alphanumeric
item with no JUSTIFIED clause or editing characters in its description.
Thus, the data movement of the STRING statement always fills the
receiving item with the sending item from left to right and with no
editing insertions.
5.1.1 Multiple Sending Items
The STRING statement can concatenate a series of sending items into one receiving item. Consider the following example:
STRING FIELD1A FIELD1B FIELD1C DELIMITED BY SIZE INTO FIELD2. |
In this sample STRING statement, FIELD1A, FIELD1B, and FIELD1C are all sending items. The compiler moves them to the receiving item (FIELD2) in the order in which they appear in the statement, from left to right, resulting in the concatenation of their values.
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. . . . |
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, because 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.
Figure 5-1 Results of the STRING Operation
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.
Remember, the remainder of the receiving item is not space-filled, as with a MOVE statement. If ADDRESS-LINE is to be printed on a mailing label, for example, the STRING statement should be preceded by the statement:
MOVE SPACES TO ADDRESS-LINE. |
This statement guarantees a space-fill to the right of the concatenated
result. Alternatively, the last item concatenated by the STRING
statement can be an item previously set to SPACES. This sending item
must either be moved under control of a delimiter other than SPACE or
use the value of POINTER and reference modification.
5.1.3 Using the POINTER Phrase
Although the STRING statement normally starts scanning at the leftmost position of the receiving item, the POINTER phrase makes it possible to start scanning at another point within the item. The scanning, however, continues left to right. Consider the following example:
MOVE 5 TO P. STRING FIELD1A FIELD1B DELIMITED BY SIZE INTO FIELD2 WITH POINTER P. |
The value of P determines the starting character position in the receiving item. In this example, the 5 in P causes the program to move the first character of FIELD1A into character position 5 of FIELD2 (the leftmost character position of the receiving item is character position 1), and leave positions 1 to 4 unchanged.
When the STRING operation is complete, P points to one character position beyond the last character replaced in the receiving item. If FIELD1A and FIELD1B are both four characters long, P contains a value of 13 (5+4+4) when the operation is complete (assuming that FIELD2 is at least 13 characters long).
Previous | Next | Contents | Index |