STRING and UNSTRING
STRING
STRING joins fields into one target, moving characters until the target is full. DELIMITED BY SIZE copies a whole field, and DELIMITED BY SPACE stops at the first space:
STRING WS-FIRST-NAME DELIMITED BY SPACE
" " DELIMITED BY SIZE
WS-LAST-NAME DELIMITED BY SIZE
INTO WS-FULL-NAME.
UNSTRING
UNSTRING splits a source field into targets, breaking at a delimiter. DELIMITED BY "," reads CSV-style input, and the delimiter itself is discarded:
UNSTRING WS-LINE DELIMITED BY ","
INTO WS-NAME WS-CITY WS-ZIP.
WITH POINTER
WITH POINTER tracks the character position, so UNSTRING can continue where the last one stopped. TALLYING counts how many fields were filled, which catches short input lines:
UNSTRING WS-LINE DELIMITED BY ","
INTO WS-A WS-B WS-C
TALLYING WS-FILLED.
The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev