Menu
CoddyTech

UNSTRING

Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 12 of 19.

UNSTRING splits one field into several at a delimiter:

UNSTRING PLACE-IN DELIMITED BY ","
    INTO CITY CTRY
END-UNSTRING.

Oslo,NO puts Oslo in CITY and NO in CTRY. Each part is moved like a MOVE: padded with spaces or cut to the field's size.

TALLYING IN N counts the fields that received a part. DELIMITED BY ALL SPACE treats a run of spaces as one delimiter, which splits words:

UNSTRING NAME-IN DELIMITED BY ALL SPACE
    INTO W1 W2 W3
    TALLYING IN N
END-UNSTRING.

A field that no part reaches keeps its old value. When lines can be short, clear the fields before each UNSTRING:

MOVE SPACES TO CITY CTRY.
challenge icon

Challenge

Easy

The program reads a line like Ada,London,36 into REC-IN. Split it at the commas into U-NAME, U-CITY and U-AGE; the program displays them.

Example input, one line per ACCEPT: Ada,London,36.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SPLIT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 REC-IN PIC X(40).
       01 U-NAME PIC X(10).
       01 U-CITY PIC X(12).
       01 U-AGE PIC X(3).
       PROCEDURE DIVISION.
           ACCEPT REC-IN.
           *> Split REC-IN here
           DISPLAY "Name: " U-NAME.
           DISPLAY "City: " U-CITY.
           DISPLAY "Age: " U-AGE.
           STOP RUN.

All lessons in Tables and Text