HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
FIELD2A is a 3-character alphanumeric item. It receives the first three characters of FIELD1 (ABC) in every operation. FIELD2B, however, runs out of characters every time before filling, as Table 5-3 illustrates.
FIELD1 PIC X(6) VALUE IS: |
FIELD2B PICTURE IS: |
FIELD2B Value After UNSTRING Operation |
---|---|---|
ABCDEF | XXXXX | DEF |
S99999 | 0024F | |
ABC246 | S9V999 | 600{ |
S9999 LEADING SEPARATE | +0246 |
The size of the data to be moved can be controlled by a delimiter, rather than by the size of the receiving item. The DELIMITED BY phrase supplies the delimiter characters.
UNSTRING delimiters can be literals, figurative constants (including ALL literal), or identifiers (identifiers can even be subscripted data names). This section describes the use of these three types of delimiters. Subsequent sections cover multiple delimiters, the COUNT phrase, and the DELIMITER phrase.
Consider the following sample UNSTRING statement with the figurative constant SPACE as a delimiter:
UNSTRING FIELD1 DELIMITED BY SPACE INTO FIELD2. |
In this example, the compiler scans the sending item (FIELD1), searching for a space character. If it encounters a space, it moves all of the scanned (nonspace) characters that precede that space to the receiving item (FIELD2). If it finds no space character, it moves the entire sending item. When the compiler has determined the size of the sending item, it moves the contents of that item following the rules for the MOVE statement, truncating or zero-filling as required.
Table 5-4 shows the results of the following UNSTRING operation that uses a literal asterisk delimiter:
UNSTRING FIELD1 DELIMITED BY "*" INTO FIELD2. |
FIELD1 PIC X(6) VALUE IS: |
FIELD2 PICTURE IS: |
FIELD2 Value After UNSTRING |
---|---|---|
XXX | ABC | |
ABCDEF | X(7) | ABCDEF |
XXX JUSTIFIED | DEF | |
****** | XXX | ### |
*ABCDE | XXX | ### |
A***** | XXX JUSTIFIED | ##A |
246*** | S9999 | 024F |
12345* | S9999 TRAILING SEPARATE | 2345+ |
2468** | S999V9 LEADING SEPARATE | +4680 |
*246** | 9999 | 0000 |
Legend: # = space
If the delimiter matches the first character in the sending item, the compiler considers the size of the sending item to be zero. The operation still takes place, however, and fills the receiving item with spaces (if it is nonnumeric) or zeros (if it is numeric).
A delimiter can also be applied to an UNSTRING statement that has multiple receiving items:
UNSTRING FIELD1 DELIMITED BY SPACE INTO FIELD2A FIELD2B. |
The compiler generates code that scans FIELD1 searching for a character that matches the delimiter. If it finds a match, it moves the scanned characters to FIELD2A and sets the scanner to the next character position to the right of the character that matched. The compiler then resumes scanning FIELD1 for a character that matches the delimiter. If it finds a match, it moves all of the characters between the character that first matched the delimiter and the character that matched on the second scan, and sets the scanner to the next character position to the right of the character that matched.
The DELIMITED BY phrase handles additional items in the same manner as it handled FIELD2B.
Table 5-5 illustrates the results of the following delimited UNSTRING operation into multiple receiving items:
UNSTRING FIELD1 DELIMITED BY "*" INTO FIELD2A FIELD2B. |
Values After UNSTRING Operation | ||
---|---|---|
FIELD1 PIC X(8) VALUE IS: |
FIELD2A PIC X(3) |
FIELD2B PIC X(3) |
ABC*DEF* | ABC | DEF |
ABCDE*FG | ABC | FG# |
A*B**** | A## | B## |
*AB*CD** | ### | AB# |
**ABCDEF | ### | ### |
A*BCDEFG | A## | BCD |
ABC**DEF | ABC | ### |
A******B | A## | ### |
Legend: # = space
The previous examples illustrate the limitations of a single-character delimiter. To overcome these limitations, a delimiter of more than one character or a delimiter preceded by the word ALL may be used.
Table 5-6 shows the results of the following UNSTRING operation using a 2-character delimiter:
UNSTRING FIELD1 DELIMITED BY "**" INTO FIELD2A FIELD2B. |
Values After UNSTRING Operation | ||
---|---|---|
FIELD1 PIC X(8) VALUE IS: |
FIELD2A PIC XXX |
FIELD2B PIC XXX JUSTIFIED |
ABC**DEF | ABC | DEF |
A*B*C*D* | A*B | ### |
AB***C*D | AB# | C*D |
AB**C*D* | AB# | *D* |
AB**CD** | AB# | #CD |
AB***CD* | AB# | CD* |
AB*****CD | AB# | ### |
Legend: # = space
Unlike the STRING statement, the UNSTRING statement accepts the ALL literal as a delimiter. When the word ALL precedes the delimiter, the action of the UNSTRING statement remains essentially the same as with one delimiter until the scanning operation finds a match. At this point, the compiler scans farther, looking for additional consecutive strings of characters that also match the delimiter item. It considers the ALL delimiter to be one, two, three, or more adjacent repetitions of the delimiter item. Table 5-7 shows the results of the following UNSTRING operation using an ALL delimiter:
UNSTRING FIELD1 DELIMITED BY ALL "*" INTO FIELD2A FIELD2B. |
Values After UNSTRING Operation | ||
---|---|---|
FIELD1 PIC X(8) VALUE IS: |
FIELD2A PIC XXX |
FIELD2B PIC XXX JUSTIFIED |
ABC*DEF* | ABC | DEF |
ABC**DEF | ABC | DEF |
A******F | A## | ##F |
A*F***** | A## | ##F |
A*CDEFG | A## | EFG |
Legend: # = space
Previous | Next | Contents | Index |