String intrinsic functions
STRING, UNSTRING and INSPECT are statements: they change fields in place.
Intrinsic functions are different: they take arguments and return a
new value, which you can display, move, compare or pass to another
function. You call them with the word FUNCTION:
IDENTIFICATION DIVISION.
PROGRAM-ID. STRFUNC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-IN PIC X(20) VALUE " Grace Hopper".
01 WS-OUT PIC X(20).
01 WS-LEN PIC 9(3).
PROCEDURE DIVISION.
DISPLAY "[" FUNCTION TRIM(WS-IN) "]"
DISPLAY "[" FUNCTION TRIM(WS-IN LEADING) "]"
DISPLAY "[" FUNCTION TRIM(WS-IN TRAILING) "]"
DISPLAY "[" FUNCTION UPPER-CASE(WS-IN) "]"
MOVE FUNCTION LOWER-CASE(FUNCTION TRIM(WS-IN)) TO WS-OUT
DISPLAY "[" WS-OUT "]"
DISPLAY "[" FUNCTION REVERSE("STRESSED") "]"
MOVE FUNCTION LENGTH(WS-IN) TO WS-LEN
DISPLAY "FIELD LENGTH: " WS-LEN
MOVE FUNCTION LENGTH(FUNCTION TRIM(WS-IN)) TO WS-LEN
DISPLAY "TEXT LENGTH: " WS-LEN
DISPLAY "[" FUNCTION CONCATENATE(
FUNCTION TRIM(WS-IN) " (" "ADMIRAL" ")") "]"
STOP RUN.
Output:
[Grace Hopper]
[Grace Hopper ]
[ Grace Hopper]
[ GRACE HOPPER ]
[grace hopper ]
[DESSERTS]
FIELD LENGTH: 020
TEXT LENGTH: 012
[Grace Hopper (ADMIRAL)]
| Function | Returns |
|---|---|
TRIM(x) |
x without leading and trailing spaces. TRIM(x LEADING) or TRIM(x TRAILING) trims one side only |
UPPER-CASE(x), LOWER-CASE(x) |
x with letters converted |
REVERSE(x) |
x backwards |
LENGTH(x) |
the number of characters in x |
CONCATENATE(a b ...) |
the arguments joined together |
Lengths: field versus text
FUNCTION LENGTH(WS-IN) is the declared size of the field, 20, no matter
what it holds. To measure the text inside, trim it first:
FUNCTION LENGTH(FUNCTION TRIM(WS-IN)). For trailing spaces only (keep an
intentional indent), use TRIM(WS-IN TRAILING).
Move numeric results into a field of your own before displaying them, as
the example does with WS-LEN. Displayed directly, a function result's
format depends on the compiler.
Functions nest
A function call can appear wherever a value of its type is allowed,
including as another function's argument.
FUNCTION LOWER-CASE(FUNCTION TRIM(WS-IN)) trims first, then
lower-cases the result. A function only produces a value: it can never
be the target of a MOVE. To change part of the result, move it into a
field first and work on the field.
Functions versus statements
Most of this overlaps with earlier lessons. UPPER-CASE does what
INSPECT ... CONVERTING does, and CONCATENATE does what STRING does.
Functions are shorter and read more naturally. The statements are older,
work on every compiler, and give you control that functions do not
(pointers, overflow detection, counts).
Compiler support
UPPER-CASE, LOWER-CASE, REVERSE and LENGTH have been standard
since 1989. TRIM arrived later, so older code does the same job with
INSPECT and reference modification loops. CONCATENATE is a newer
addition that not every mainframe compiler supports: GnuCOBOL has it,
but check your shop's compiler before relying on it. STRING always
works.
On the job
Names and addresses from online channels arrive in any case, with
stray spaces. Standardising them (trimmed, one consistent case) before
you store or compare them saves you from duplicate customers such as
grace hopper and GRACE HOPPER.
Your task
Names typed into the online account-opening form arrive in any case with
stray spaces around them. Read one line (WS-IN, PIC X(40)) holding a
first and last name, e.g. gRACE hopper, and display:
UPPER: GRACE HOPPER
NAME: Grace Hopper
LENGTH: 12
UPPERis the trimmed name in capitals.NAMEis the trimmed name in title case: the first letter of each word upper case, everything else lower case. Words are separated by single spaces.LENGTHis the length of the trimmed name, as 2 digits.
The starter declares WS-NAME, a loop counter WS-I, WS-LEN and
WS-PREV (the previous character).