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-nis a literal or data item that is not numeric.delimiter-nis an alphanumeric literal.integer-data-nis a numeric data item designed to hold an integer.statement-nis an imperative statement.
General Rules:
- There is no limit to the number of target identifiers that may be listed.
- The
DELIMITED BYclause provides a way to identify a substring of anidentifier-1, for purposes of theUNSTRINGoperation. - The
DELIMITED BY delimiter-1clause causes theUNSTRINGoperation to stop copying data fromidentifier-1whendelimiter-1is encountered.Delimiter-1is not copied to thetarget-string. - The
COUNTphrase counts the number of characters in a substring. - The
WITH POINTERclause provides a way to set a position in the source string from which theUNSTRINGstatement will copy the selected source using apointer-variable. - The
WITH POINTERclause causes an explicitpointer-variableto be referenced by theUNSTRINGoperation when it begins to copy data from a source string to atarget-string. The value of thepointer-variableis used as the position in thetarget-stringat which to copy the data. - When the
WITH POINTERclause is used, thepointer-variablemust be set to a positive integer value before theUNSTRINGstatement is executed. Most commonly, thepointer-variableis initialized to 1, indicating that theUNSTRINGstatement should begin parsing the source string from the first byte. - The
pointer-variableis automatically incremented by 1 for each character that is parsed in the source string. - If there is no
WITH POINTERclause, the position at which the source string is copied into the nexttarget-stringis the first character, or, the next character position after the last delimiter character encountered in the original source string. - The
TALLYphrase counts the number of substrings that have been created by theUNSTRINGstatement. - The
ON OVERFLOWcondition is triggered when thepointer-variabledoes not contain a positive value, or when the length oftarget-stringis less than the length of the substring identified in theUNSTRINGstatement. - When an
ON OVERFLOWcondition exists, theUNSTRINGis terminated, and theON OVERFLOW statement-1is executed. - The
NOT ON OVERFLOWcondition exists when theUNSTRINGstatement has completed, and theON OVERFLOWcondition does not exist. - When a
NOT ON OVERFLOWcondition exists,statement-2is executed. - The
ALLphrase 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.