Using the STRING, UNSTRING, and INSPECT Statements
5.2 Separating Data Using the UNSTRING Statement
5.2.2.1 Multiple Delimiters
The UNSTRING statement scans a sending item, searching for a match from a
list of delimiters. This list can contain ALL delimiters and delimiters of various
sizes. Delimiters in the list must be connected by the word OR.
The following sample statement unstrings a sending item into three receiving
items. The sending item consists of three strings separated by one of the
following: ( 1 ) any number of spaces, ( 2 ) a comma followed by a single space,
( 3 ) a single comma, ( 4 ) a tab character, or ( 5 ) a carriage-return character. The
comma and space must precede the single comma in the list if the comma and
space are to be recognized.
UNSTRING FIELD1 DELIMITED BY ALL SPACE
OR ", "
OR ","
OR TAB
OR CR
INTO FIELD2A FIELD2B FIELD2C.
Table 5–9 shows the potential of this statement. The tab and carriage-return
characters represent single-character items containing the ASCII horizontal tab
and carriage-return characters.
Table 5–9 Results of Multiple Delimiters
FIELD1
PIC X(12)
FIELD2A
PIC XXX
FIELD2B
PIC 9999
FIELD2C
PIC XXX
A,0,C
Return
A## 0000 C##
A
Tab
456, E A## 0456 E##
A 3 9 A## 0003 9##
A
Tab Tab
B
Return
A## 0000 B##
A,,C A## 0000 C##
ABCD, 4321,Z ABC 4321 Z##
Legend: # = space
5.2.3 Using the COUNT Phrase
The COUNT phrase keeps track of the size of the sending string and stores the
length in a user-supplied data area.
The length of a delimited sending item can vary from zero to the full length of
the item. Some programs require knowledge of this length. For example, some
data is truncated if it exceeds the size of the receiving item, so the program’s logic
requires this information.
The COUNT phrase follows the receiving item. Consider the following example:
UNSTRING FIELD1 DELIMITED BY ALL "*"
INTO FIELD2A COUNT IN COUNT2A
FIELD2B COUNT IN COUNT2B
FIELD2C.
The compiler generates code that counts the number of characters between the
leftmost position of FIELD1 and the first asterisk in FIELD1 and places the count
into COUNT2A. The delimiter is not included in the count because it is not a part
of the string. The data preceding the first asterisk is then moved into FIELD2A.
5–12 Using the STRING, UNSTRING, and INSPECT Statements