UNSTRING statement
UNSTRING statement

The UNSTRING statement used to separate single string to multiple based on the delimiter provided in the UNSTRING. It needs at least two destination identifier or literals. It can only separate alphabetic and alpha-numeric items. END-STRING, DELIMITED BY, TALLYING, WITH POINTER, ON OVERFLOW and NOT ON OVERFLOW clause is optional in UNSTRING usage.

UNSTRING statement Syntax

UNSTRING 
{Variable-1} [DELIMITED BY SIZE/SPACE/specified delimiter]
INTO {Variable-A} [DELIMITER IN delimete-1][COUNT IN counter-1] {Variable-B} [DELIMITER IN delimete-2][COUNT IN 
counter-2]
 ……..
 ……..
[WITH POINTER ws-pointer]
[TALLYING ON identifier-t]
[ON OVERFLOW imperative-statements-1]
[NOT ON OVERFLOW imperative-statements-2]
[END-UNSTRING.]

COUNT IN: COUNT IN clause associated with a particular destination data item. COUNT IN holds the count of no of character passed to the particular destination identifier/data item.

TALLYING: TALLYING clause holds the count of destination strings affected by UNSTRING. TALLYING clause associated with all the destination data items.

UNSTRING Statement Example

UNSTRING NAME-OUT DELIMITED BY SPACE
     INTO FIRST-NAME
                LAST-NAME
                INITIAL
     ON OVERFLOW DISPLAY “ERROR FOUND”
     NOT ON OVERFLOW DISPLAY “SUCCESSFUL”
END-UNSTRING.

In the Data Division, the user has defined the following input record to be acted upon by the UNSTRING statement:

01 INV-RCD.
05 CONTROL-CHARS PIC XX.
05 ITEM-INDENT PIC X(2 ).
05 FILLER PIC X.
05 INV-CODE PIC X(1 ).
05 FILLER PIC X.
05 NO-UNITS PIC 9(6).
05 FILLER PIC X.
05 PRICE-PER-M PIC 99999.
05 FILLER PIC X.
05 RTL-AMT PIC 9(6).99.

The next two records are defined as receiving fields for the UNSTRING statement. DISPLAY-REC is to be used for printed output. WORK-REC is to be used for further internal processing.

01 DISPLAY-REC
05 INV-NO PIC X(6).
05 FILLER PIC X VALUE SPACE
05 ITEM-NAME PIC X(2 ).
05 FILLER PIC X VALUE SPACE
05 DISPLAY-DOLS PIC 9(6).
01 WORK-REC 05 M-UNITS PIC 9(6).
05 FIELD-A PIC 9(6).
05 WK-PRICE REDEFINES FIELD-A PIC 9999V99.
05 INV-CLASS PIC X(3).

The user has also defined the following fields for use as control fields in the UNSTRING statement.

 01 DBY-1 PIC X, VALUE IS ".".
01 CTR-1 PIC 99, VALUE IS ZERO.
01 CTR-2 PIC 99, VALUE IS ZERO.
01 CTR-3 PIC 99, VALUE IS ZERO.
01 CTR-4 PIC 99, VALUE IS ZERO.
01 DLTR-1 PIC X.
01 DLTR-2 PIC X.
01 CHAR-CT PIC 99, VALUE IS 3.
01 FLDS-FILLED PIC 99, VALUE IS ZERO.

In the Procedure Division, the user writes the following UNSTRING statement to move subfields of INV-RCD to the subfields of DISPLAY-REC and WORK-REC:

UNSTRING INV-RCD
DELIMITED BY ALL SPACES
  OR "/"
  OR DBY-1
INTO ITEM-NAME COUNT IN CTR-1,
INV-NO DELIMITER IN DLTR-1
COUNT IN CTR-2,
      INV-CLASS,
      M-UNITS COUNT IN CTR-3,
      FIELD-A,
DISPLAY-DOLS DELIMITER IN DLTR-2
COUNT IN CTR-4
WITH POINTER CHAR-CT
TALLYING IN FLDS-FILLED
ON OVERFLOW
GO TO UNSTRING-COMPLETE.

Before the UNSTRING statement is issued, the user places the value 3 in the CHAR-CT (the pointer item), so as not to work with the two control characters at the beginning of INV-RCD. In DBY-1, a period is placed for use as a delimiter, and in FLDS-FILLED (the tallying item) the value 0 is placed. The following data is then read into INV-RCD

ZYFOUR─PENNY─NAILS 707890/BBA 475120 00122 000379.50

When the UNSTRING statement is executed, the following actions take place:

  1. Positions 3 through 18 (FOUR-PENNY-NAILS) of INV-RCD are placed in
    ITEM-NAME, left-justified within the area, and the unused character positions are padded with spaces. The value 16 is placed in CTR-1.
  2. Because ALL SPACES is specified as a delimiter, the five contiguous SPACE characters are considered to be one occurrence of the delimiter.
  3. Positions 24 through 29 (707890) are placed in INV-NO. The delimiter character / is placed in DLTR-1, and the value 6 is placed in CTR-2.
  4. Positions 31 through 33 are placed in INV-CLASS. The delimiter is a SPACE, but because no field has been defined as a receiving area for delimiters, SPACE is merely bypassed.
  5. Positions 35 through 40 (475120) are examined and are placed in M-UNITS.
  6. The delimiter is a SPACE, but because no receiving field has been defined as a receiving area for delimiters, SPACE is bypassed. The value 6 is placed
    in CTR-3.
  7. Positions 42 through 46 (00122) are placed in FIELD-A and right-justified within the area. The leftmost character position is filled with a 0 (zero). The delimiter is a SPACE, but because no field has been defined as a receiving area for delimiters, SPACE is bypassed.
  8. Positions 48 through 53 (000379) are placed in DISPLAY-DOLS. The period delimiter character is placed in DLTR-2, and the value 6 is placed in CTR-4.
  9. Because all receiving fields have been acted upon and two characters of data in INV-RCD have not been examined, the ON OVERFLOW exit is taken, and execution of the UNSTRING statement is completed.

At the end of execution of the UNSTRING statement, DISPLAY-REC contains the following data:

707890 FOUR-PENNY-NAILS 000379
WORK-REC contains the following data: 475120000122BBA

CHAR-CT (the pointer field) contains the value 55, and FLD-FILLED (the tallying field) contains the value 6.

COBOL Blog: Click Here IBM Reference:Click Here

Scroll to Top