UNSTRING Statement
UNSTRING Statement
- UNSTRING statement takes a single string, breaks it down into several separate strings, and places them into the variables.
- It breaks the strings into multiple stings based on the delimiter.
- It does the task of several MOVE statements.
- UNSTRING requires a minimum of two receiving variables.
- It is applicable only to alphabetic and alpha-numeric items and not applicable to numeric items.
Syntax -
---+----2----+----3----+----4----+----5
UNSTRING source-string
[DELIMITED BY delimiter1]
INTO target-string-1 [target-string-2 ...]
[TALLYING IN counter-name]
[ON OVERFLOW statements-block-1]
[NOT ON OVERFLOW statements-block-2]
[END-UNSTRING].
Note! All statements coded in [ ] are optional.
Parameters -
- source-string - Specifies the input string that we want to break down.
- DELIMITED BY delimiter1 - The delimiter1 is used to specify where to split the string. If it's not coded, then each character is considered separately.
- INTO target-item-1 [target-item-2 ...] - Specifies the target variables where the divided strings should be placed.
- TALLYING IN - Count the number of characters transferred to the target items.
- ON OVERFLOW statements-block-1 - Specifies the statements block executed when ON OVERFLOW occurs.
- NOT ON OVERFLOW statement-block-2 - Specifies the statements block executed when the STRING operation is successful.
- END-UNSTRING - Explicit scope terminator for the UNSTRING statement.
Practical Example -
Scenario - Split the string using UNSTRING statement.
Code -
----+----1----+----2----+----3----+----4----+----5----
...
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-INPUT PIC X(70) VALUE
"Mainframestechhelp, is a Mainframe Community".
05 WS-OUTPUT1 PIC X(30).
05 WS-OUTPUT2 PIC X(40).
...
PROCEDURE DIVISION.
UNSTRING WS-INPUT DELIMITED BY ","
INTO WS-OUTPUT1, WS-OUTPUT2
ON OVERFLOW DISPLAY "ERROR OCCURED"
NOT ON OVERFLOW
DISPLAY "WS-OUTPUT1: ", WS-OUTPUT1
DISPLAY "WS-OUTPUT2: ", WS-OUTPUT2
END-UNSTRING.
...
Output -
WS-OUTPUT1: Mainframestechhelp WS-OUTPUT2: is a Mainframe Community