UNSTRING Statement
The UNSTRING
statement separates parts of an existing string that share known delimiters into different data items.
General Format:
UNSTRING identifier-1
[ DELIMITED BY [ALL] delimiter-1
[ OR [ALL] delimiter-2 ] ... ]
INTO { identifier-2 [ DELIMITER in delimiter-2 ]
[ COUNT IN integer-data-1 ] } ...
[ WITH POINTER integer-data-2 ]
[ TALLYING IN integer-data-3 ]
[ ON OVERFLOW statement-1 ]
[ NOTON OVERFLOW statement-2 ]
[ END-UNSTRING ]
Syntax:
identifier-n
is a literal or data item that is not numeric.delimiter-n
is an alphanumeric literal.integer-data-n
is a numeric data item designed to hold an integer.statement-n
is an imperative statement.
General Rules:
- There is no limit to the number of target identifiers that may be listed.
- The
DELIMITED BY
clause provides a way to identify a substring of anidentifier-1
, for purposes of theUNSTRING
operation. - The
DELIMITED BY delimiter-1
clause causes theUNSTRING
operation to stop copying data fromidentifier-1
whendelimiter-1
is encountered.Delimiter-1
is not copied to thetarget-string
. - The
COUNT
phrase counts the number of characters in a substring. - The
WITH POINTER
clause provides a way to set a position in the source string from which theUNSTRING
statement will copy the selected source using apointer-variable
. - The
WITH POINTER
clause causes an explicitpointer-variable
to be referenced by theUNSTRING
operation when it begins to copy data from a source string to atarget-string
. The value of thepointer-variable
is used as the position in thetarget-string
at which to copy the data. - When the
WITH POINTER
clause is used, thepointer-variable
must be set to a positive integer value before theUNSTRING
statement is executed. Most commonly, thepointer-variable
is initialized to 1, indicating that theUNSTRING
statement should begin parsing the source string from the first byte. - The
pointer-variable
is automatically incremented by 1 for each character that is parsed in the source string. - If there is no
WITH POINTER
clause, the position at which the source string is copied into the nexttarget-string
is the first character, or, the next character position after the last delimiter character encountered in the original source string. - The
TALLY
phrase counts the number of substrings that have been created by theUNSTRING
statement. - The
ON OVERFLOW
condition is triggered when thepointer-variable
does not contain a positive value, or when the length oftarget-string
is less than the length of the substring identified in theUNSTRING
statement. - When an
ON OVERFLOW
condition exists, theUNSTRING
is terminated, and theON OVERFLOW statement-1
is executed. - The
NOT ON OVERFLOW
condition exists when theUNSTRING
statement has completed, and theON OVERFLOW
condition does not exist. - When a
NOT ON OVERFLOW
condition exists,statement-2
is executed. - The
ALL
phrase indicates that a sequence of delimiters should be treated as a singled delimiter.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. UNSTRING-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ADDRESS-1
PIC X(40) VALUE"231/RUE SAINT-HONORE/75001 PARIS/FRANCE/".
01 ADDRESS-2.
05 ADDRESS-NUM PIC X(5) VALUE SPACES.
05 ADDRESS-NAME PIC X(20) VALUE SPACES.
05 ADDRESS-CITY PIC X(20) VALUE SPACES.
05 ADDRESS-COUNTRY PIC X(20) VALUE SPACES.
77 COUNTER-1 PIC 99 VALUE 0.
77 COUNTER-2 PIC 99 VALUE 0.
77 COUNTER-3 PIC 99 VALUE 0.
77 COUNTER-4 PIC 99 VALUE 0.
77 TALLY-1 PIC 99 VALUE 0.
01 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
UNSTRING ADDRESS-1 DELIMITED BY "/"
INTO ADDRESS-NUM, COUNT IN COUNTER-1,
ADDRESS-NAME, COUNT IN COUNTER-2,
ADDRESS-CITY, COUNT IN COUNTER-3,
ADDRESS-COUNTRY, COUNT IN COUNTER-4
TALLYING IN TALLY-1.
DISPLAY "THE STREET #: " LINE 4 COL 10.
DISPLAY ADDRESS-NUM LINE 4 COL 28.
DISPLAY COUNTER-1 LINE 4 COL 50.
*
DISPLAY "THE STREET NAME: " LINE 5 COL 10.
DISPLAY ADDRESS-NAME LINE 5 COL 28.
DISPLAY COUNTER-2 LINE 5 COL 50.
DISPLAY "THE CITY: " LINE 6 COL 10.
DISPLAY ADDRESS-CITY LINE 6 COL 28.
DISPLAY COUNTER-3 LINE 6 COL 50.
DISPLAY "THE COUNTRY: " LINE 7 COL 10.
DISPLAY ADDRESS-COUNTRY LINE 7 COL 28.
DISPLAY COUNTER-4 LINE 7 COL 50.
DISPLAY "TALLY: " LINE 9 COL 10.
DISPLAY TALLY-1 LINE 9 COL 28.
DISPLAY "UNSTRING-1 FINISHED!" LINE 12 COL 10.
ACCEPT DUMMY LINE 12 COL 30.
STOP RUN.