Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Data Manipulation

String Handling: STRING, UNSTRING and INSPECT

Handle text in COBOL: STRING to join with POINTER and ON OVERFLOW, UNSTRING to split on delimiters with COUNT and TALLYING, and INSPECT to count, replace and convert characters.

COBOL has no string type: text lives in fixed-size PIC X(n) fields. Three statements do the real work. STRING joins pieces, UNSTRING splits text apart, and INSPECT counts or changes characters. They are powerful but have quirks, because they work on fixed-length fields and do not clear or pad the receiving field the way MOVE does.

STRING: joining text

COBOL
       01 FULL-NAME   PIC X(30).
       01 FIRST-NAME  PIC X(10) VALUE "Ada".
       01 LAST-NAME   PIC X(10) VALUE "Lovelace".
       ...
           MOVE SPACES TO FULL-NAME
           STRING FIRST-NAME DELIMITED BY SPACE
                  " "        DELIMITED BY SIZE
                  LAST-NAME  DELIMITED BY SPACE
               INTO FULL-NAME
           END-STRING

Each source is followed by DELIMITED BY: a character (SPACE, ",", a literal) at which to stop copying, or SIZE to copy the whole item. FIRST-NAME is "Ada "; delimiting by SPACE copies only Ada. The result in FULL-NAME is Ada Lovelace.

Key behaviours:

  • STRING writes from the start of the receiver and does not clear the rest. That is why you first MOVE SPACES to it.
  • If the data is too long for the receiver, copying stops and the ON OVERFLOW branch runs.
  • WITH POINTER P starts writing at position P (1-based) and leaves P pointing after the last character written, so you can build a line in several STRING statements.
COBOL
           MOVE 1 TO PTR
           STRING "ID=" DELIMITED BY SIZE
                  ID-TEXT DELIMITED BY SIZE
               INTO OUT-LINE WITH POINTER PTR
               ON OVERFLOW DISPLAY "line too long"
           END-STRING
           STRING ";" DELIMITED BY SIZE
               INTO OUT-LINE WITH POINTER PTR
           END-STRING
Python STRING with POINTER (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

UNSTRING: splitting text

COBOL
       01 INPUT-LINE   PIC X(40) VALUE "Smith,John,42,Boston".
       01 F-LAST       PIC X(10).
       01 F-FIRST      PIC X(10).
       01 F-AGE        PIC X(3).
       01 F-CITY       PIC X(15).
       01 FIELD-COUNT  PIC 99.
       ...
           UNSTRING INPUT-LINE
               DELIMITED BY ","
               INTO F-LAST F-FIRST F-AGE F-CITY
               TALLYING IN FIELD-COUNT
           END-UNSTRING

Each piece goes to the next INTO field following the MOVE rules (left-justified, padded or cut). Extra options:

  • DELIMITED BY ALL "," treats a run of commas as one delimiter.
  • DELIMITED BY "," OR ";" accepts several delimiters.
  • INTO F-LAST COUNT IN LAST-LEN DELIMITER IN DELIM-CHAR records the size of the piece before the delimiter and which delimiter ended it.
  • WITH POINTER starts at a given position, so you can parse one field per statement.
  • TALLYING IN counts the receiving fields filled. If a field is not reached because the text ran out, it is left unchanged, so initialise your fields first.
  • ON OVERFLOW runs when there are more pieces than receiving fields, or the pointer is out of range.
Python UNSTRING a CSV line (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

INSPECT: counting and replacing

INSPECT looks at every character of a field.

TALLYING

COBOL
           MOVE ZERO TO A-COUNT
           INSPECT TEXT-FIELD TALLYING A-COUNT FOR ALL "A"
           INSPECT TEXT-FIELD TALLYING SPACES-LEADING FOR LEADING SPACE
           INSPECT TEXT-FIELD TALLYING CHAR-COUNT FOR CHARACTERS

ALL counts every occurrence, LEADING counts only a run at the start, CHARACTERS counts every character. You must initialise the counter (MOVE ZERO): INSPECT adds to it.

REPLACING

COBOL
           INSPECT PHONE REPLACING ALL "-" BY SPACE
           INSPECT CODE  REPLACING FIRST "0" BY "9"
           INSPECT AMOUNT-TEXT REPLACING LEADING SPACE BY ZERO

The replaced text must have the same length as the original: one character for one character, or an equal-sized literal.

CONVERTING

CONVERTING maps characters one for one, ideal for case conversion on older compilers:

COBOL
           INSPECT NAME-FIELD CONVERTING
               "abcdefghijklmnopqrstuvwxyz" TO
               "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Any of the above may be limited with BEFORE INITIAL x or AFTER INITIAL x to act on only part of the field. Modern code can use FUNCTION UPPER-CASE(...) instead (see intrinsic functions).

Python INSPECT (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Forgetting MOVE SPACES to the receiver before STRING and ending up with leftover characters.
  • DELIMITED BY SPACE on text that contains spaces (it cuts at the first one: use SIZE or trim first).
  • Not initialising the TALLYING counter.
  • Using INSPECT REPLACING with literals of different lengths.
  • Building text in a field that is too short without an ON OVERFLOW branch.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do you usually MOVE SPACES to the receiver before a STRING?

    STRING writes only as many characters as it needs and leaves the rest of the field as it was.
  2. 2. What does UNSTRING ... TALLYING IN N count?

    The tally counts the INTO fields that received data.
  3. 3. Which statement replaces every dash in PHONE by a space?

    INSPECT ... REPLACING ALL old BY new does a same-length substitution.
  4. 4. What must you do before INSPECT ... TALLYING ...?

    INSPECT adds to the counter, so it must start at zero.

Summary

  • STRING joins sources into one field (DELIMITED BY, POINTER, ON OVERFLOW) and leaves unused positions untouched.
  • UNSTRING splits on delimiters into several fields (COUNT IN, TALLYING IN, ALL, OR).
  • INSPECT counts (TALLYING), replaces (REPLACING) and maps characters (CONVERTING), optionally BEFORE/AFTER INITIAL.
  • Fields are fixed-size, so clear receivers first and watch lengths.

Next: Reference Modification and Intrinsic Functions