ContentsIndexPreviousNext

Code Examples

Use UNSTRING to decompose strings containing multiple data elements. For example, a string data item might contain a person's name, using commas to separate the name fields: "last-name,first-name,middle-initial". Using UNSTRING, and specifying "," (comma) as the delimiter, you could separate the name string into three data items, each containing an element of the full name.

Code example 1:

Assume the following data items:

01  CUSTOMER-NAME    PIC X(40)  VALUE ALL SPACES.
01  LAST-NAME        PIC X(25)  VALUE ALL SPACES.
01  FIRST-NAME       PIC X(14)  VALUE ALL SPACES.
01  MIDDLE-I         PIC X      VALUE ALL SPACES.
{ . . . }
PROCEDURE DIVISION.
{ . . . }
 DISPLAY 'Enter name: LAST,FIRST,MIDDLE-INITIAL'.
 DISPLAY 'Use a comma to separate each name entry'.
    ACCEPT CUSTOMER-NAME.

{ . . . }

UNSTRING CUSTOMER-NAME
   DELIMITED BY ","
   INTO LAST-NAME,   |characters to first comma
        FIRST-NAME,  |characters to second comma
        MIDDLE-I     |gets only the first character
                     |of the remaining string.  No
                     |overflow is raised.
                     |See general rule 12.
   ON OVERFLOW
      DISPLAY 'OVERFLOW on UNSTRING'
END-UNSTRING.

For code examples 2 and 3 assume the following data items:

01  COLOR-LIST  PIC X(22) VALUE "RED:BLUE/GREEN  YELLOW".
01  COLOR-1     PIC X(6)  VALUE ALL SPACES.
01  COLOR-2     PIC X(6)  VALUE ALL SPACES.
01  COLOR-3     PIC X(6)  VALUE ALL SPACES.
01  COLOR-4     PIC X(6)  VALUE ALL SPACES.
01  DELIMIT-1   PIC X(3)  VALUE ALL SPACES.
01  COUNT-1     PIC 9     VALUE 0

Code example 2:

UNSTRING COLOR-LIST
   DELIMITED BY ":" OR "/" OR ALL SPACE
*ALL SPACE treats contiguous spaces
*as one delimiter.
   INTO COLOR-1,
        COLOR-2,
        COLOR-3,
        COLOR-4
END-UNSTRING.
*COLOR-1 = "RED   "
*COLOR-2 = "BLUE  "
*COLOR-3 = "GREEN "
*COLOR-4 = "YELLOW"

Code example 3:

MOVE 0 TO COUNT-1.

UNSTRING COLOR-LIST
   DELIMITED BY ":" OR "/" OR ALL SPACE
*DELIMIT-1 and COUNT-1 will hold only
*the values associated with COLOR-1.
   INTO COLOR-1
         DELIMITER IN DELIMIT-1
         COUNT IN COUNT-1,
         COLOR-2,
         COLOR-3,
         COLOR-4
   ON OVERFLOW
      DISPLAY "overflow: unstring colors"
   NOT ON OVERFLOW
*do when UNSTRING succeeds.
      PERFORM SORT-COLORS
END-UNSTRING.
*COLOR-1 = "RED   "
*COLOR-2 = "BLUE  "
*COLOR-3 = "GREEN "
*COLOR-4 = "YELLOW"
*DELIMIT-1 = ":  "
*COUNT-1 = 3 count-1 holds the number of characters in RED

Code example 4:

When the string does not contain delimiters between the data elements, but the size and position of each string data element is known, the string can be deconstructed without a DELIMITED BY phrase.

Assume the following data items:

01  COLOR-LIST   PIC X(7) VALUE "REDBLUE".
01  COLOR-1      PIC X(3) VALUE ALL SPACES.
01  COLOR-2      PIC X(4) VALUE ALL SPACES.
{ . . . }
PROCEDURE DIVISION.
{ . . . }
UNSTRING COLOR-LIST
   INTO COLOR-1,
*first substring must be three characters.
        COLOR-2
*second substring must be four characters.
END-UNSTRING.
*COLOR-1 = "RED"
*COLOR-2 = "BLUE"

Code example 5:

Use POINTER and a PERFORM loop to extract and process string elements.

Assume the following data items:

01  COLOR-LIST       PIC X(21)  VALUE "RED BLUE GREEN YELLOW".
01  COLOR-LIST-SIZE  PIC 999.
01  COLOR-1          PIC X(6)   VALUE SPACES.
01  STRING-PTR       PIC 99.
01  FLAGS.
    05  COLOR-STRING-EMPTY   PIC X VALUE "N".
        88 NO-MORE-COLORS          VALUE "Y".
{ . . . }
PROCEDURE DIVISION.
{ . . . }
*string pointer must be initialized
MOVE 1 TO STRING-PTR.
SET COLOR-LIST-SIZE TO SIZE OF COLOR-LIST.
PERFORM PROCESS-COLOR UNTIL NO-MORE-COLORS.
{ . . . }
PROCESS-COLOR.
   UNSTRING COLOR-LIST
      DELIMITED BY ALL SPACE
      INTO COLOR-1
      POINTER STRING-PTR
      ON OVERFLOW
*An OVERFLOW condition will be raised every time
*through the loop, except when extracting the last
*substring.  When the overflow is the result of
*having unexamined characters at the end of the
*input string, take no action.  When the overflow
*is due to the pointer value exceeding the length
*of the string, set COLOR-STRING-EMPTY.
         IF STRING-PTR > COLOR-LIST-SIZE THEN
            MOVE "Y" TO COLOR-STRING-EMPTY
         END-IF
*process the value
   PERFORM STORE-COLOR-1
*initialize COLOR1 before fetching the next color
   MOVE SPACES INTO COLOR-1
   END-UNSTRING.