TRIM, UPPER-CASE and LOWER-CASE
A COBOL field is padded, so a name that is five letters long arrives with seven spaces if its picture says twelve. The intrinsic functions are the way to work with the value instead of the field: TRIM removes the padding at the edge, UPPER-CASE and LOWER-CASE change the letters, and LENGTH measures the result.
FUNCTION TRIM
One field with two leading spaces and two trailing ones, and the three forms of the function:
IDENTIFICATION DIVISION.
PROGRAM-ID. FUNCS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME-F PIC X(12) VALUE " Ada Lovel".
01 WORD-F PIC X(8) VALUE "london".
01 LEN-F PIC 9(2) VALUE 0.
01 UPPER-F PIC X(12) VALUE SPACES.
PROCEDURE DIVISION.
DISPLAY "[" FUNCTION TRIM(NAME-F) "]".
DISPLAY "[" FUNCTION TRIM(NAME-F LEADING) "]".
DISPLAY "[" FUNCTION TRIM(NAME-F TRAILING) "]".
COMPUTE LEN-F = FUNCTION LENGTH(FUNCTION TRIM(NAME-F)).
DISPLAY "length " LEN-F.
MOVE FUNCTION UPPER-CASE(WORD-F) TO UPPER-F.
DISPLAY "upper [" UPPER-F "]".
DISPLAY "lower [" FUNCTION LOWER-CASE(UPPER-F) "]".
STOP RUN.
cobc -x -free -Wall -o funcs funcs.cob
./funcs
[Ada Lovel]
[Ada Lovel ]
[ Ada Lovel]
length 09
upper [LONDON ]
lower [london ]
With no argument the function trims both ends; LEADING keeps the trailing spaces and TRAILING keeps the leading ones. The brackets in the output make the difference visible, and they are the reason a debug DISPLAY should always have them.
LENGTH after a TRIM
LENGTH of a field is the width of its picture, as the lesson on alphanumeric fields shows. Of a trimmed value it is the length of the value, which is what a program usually means: nine for "Ada Lovel", spaces inside included, and not twelve.
The combination is the idiomatic answer to "how long is this name". A STRING of the trimmed value then writes exactly the letters and no padding, and the pointer after it is the length as well, which is a second way to get the same number.
UPPER-CASE and LOWER-CASE
The two functions return the argument with the letters changed case, and like every function in COBOL they return a value of the same length as the argument: eight bytes in, eight bytes out, so the spaces come back with the letters. That is why the upper and lower lines both show the padding of WORD-F.
They are the modern spelling of the CONVERTING statement from two lessons ago. A program for a compiler of the 1985 standard uses INSPECT, a program for a 2002 compiler can use the function, and both are portable across GnuCOBOL and a mainframe compiler of the last twenty years.
A function is a value, not a field
A function may stand where a value may stand: in a DISPLAY, a MOVE source, a COMPUTE expression, a condition. It may not be a receiver, because it has no storage:
IDENTIFICATION DIVISION.
PROGRAM-ID. RECV.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME-F PIC X(12) VALUE " Ada Lovel".
PROCEDURE DIVISION.
MOVE "X" TO FUNCTION TRIM(NAME-F).
DISPLAY NAME-F.
STOP RUN.
cobc -x -free -Wall -o recv recv.cob
recv.cob:9: error: syntax error, unexpected FUNCTION TRIM
7 |
8 | PROCEDURE DIVISION.
9 > MOVE "X" TO FUNCTION TRIM(NAME-F).
10 | DISPLAY NAME-F.
11 | STOP RUN.
The message names the function as the token it did not expect after TO, which is the compiler saying that a receiving position needs a field. A program that wants to change the field itself moves the result back into it: MOVE FUNCTION TRIM(NAME-F) TO NAME-F is a legal statement and does the trimming in place.
Exercises
- Print the length of the name before and after the trim. Which number did the function remove, and why is it two and not four?
- Trim a field of spaces and print the result between brackets. What is between them?
- Use UPPER-CASE on a name with a lowercase letter in the middle and print it. Which letters changed?
- Write the same trim as a MOVE back into the field and print the field. Does the field get shorter?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does FUNCTION TRIM remove with no argument?
Answer
Spaces at both ends of the value
The default trims both ends; LEADING and TRAILING keep the other end as it is.
-
What does FUNCTION LENGTH of a trimmed value return?
Answer
The number of characters of the value, without the trimmed spaces
The function returns a value whose length is the trimmed one, so LENGTH measures what a reader would count.
-
Can a function call be the receiver of a MOVE?
Answer
No: a function returns a value and has no storage to write to
The compiler expects a field after TO and reports the function as an unexpected token.
-
What does FUNCTION TRIM(X LEADING) keep?
Answer
The trailing spaces, and it removes the leading ones
LEADING names the end to trim, so the other end of the value is left alone.
-
What does this program print? (The field is 12 bytes and holds two spaces, nine characters and one space.)
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(12) VALUE " Ada Lovel". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(NAME-F) "]". STOP RUN.Answer
[Ada Lovel]
The default trims both ends, so neither the two leading spaces nor the one at the end survive.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(12) VALUE " Ada Lovel". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(NAME-F LEADING) "]". STOP RUN.Answer
[Ada Lovel ]
LEADING removes the two spaces at the front and keeps the one at the end of the value.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(12) VALUE " Ada Lovel". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(NAME-F TRAILING) "]". STOP RUN.Answer
[ Ada Lovel]
TRAILING removes the space at the end and keeps the two at the front of the value.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(12) VALUE " Ada Lovel". 01 LEN-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. COMPUTE LEN-F = FUNCTION LENGTH(FUNCTION TRIM(NAME-F)). DISPLAY "[" LEN-F "]". STOP RUN.Answer
[09]
The trimmed value is Ada, a space and Lovel, which is nine characters.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 WORD-F PIC X(8) VALUE "london". 01 UPPER-F PIC X(8) VALUE SPACES. PROCEDURE DIVISION. MOVE FUNCTION UPPER-CASE(WORD-F) TO UPPER-F. DISPLAY "[" UPPER-F "]". STOP RUN.Answer
[LONDON ]
The function changes the six letters and keeps the two spaces of the field, so the receiver is eight bytes again.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(12) VALUE " Ada Lovel". PROCEDURE DIVISION. MOVE FUNCTION TRIM(NAME-F) TO NAME-F. DISPLAY "[" NAME-F "]". STOP RUN.Answer
[Ada Lovel ]
The trimmed value is written back into the same field, which is twelve bytes, so the right side is padded again and the field keeps its width.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev