Alphanumeric fields and padding
A text field in COBOL has a fixed length and no terminator. There is no null byte at the end and no length to carry, so a shorter value is padded to the picture with spaces, and those spaces are part of the value. Most of what surprises a programmer in text handling follows from that one sentence.
The length is the picture
A four-byte field given a two-byte value holds four bytes, and a comparison with the two-byte value still succeeds, because the literal is padded as well:
IDENTIFICATION DIVISION.
PROGRAM-ID. PAD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-FLD PIC X(4) VALUE "AB".
01 B-FLD PIC X(2) VALUE "AB".
01 C-FLD PIC X(4) VALUE "AB ".
PROCEDURE DIVISION.
DISPLAY "[" A-FLD "] len " LENGTH OF A-FLD.
IF A-FLD = "AB"
DISPLAY "equals literal AB"
ELSE
DISPLAY "not equal to literal AB"
END-IF.
IF A-FLD = B-FLD
DISPLAY "equals X(2) field"
ELSE
DISPLAY "not equal to X(2) field"
END-IF.
IF A-FLD = C-FLD
DISPLAY "equals X(4) field"
ELSE
DISPLAY "not equal to X(4) field"
END-IF.
STOP RUN.
cobc -x -free -Wall -o pad pad.cob
./pad
[AB ] len 4
equals literal AB
equals X(2) field
equals X(4) field
The first line is the size, and the four characters between the brackets are two letters and two spaces. LENGTH OF returns 4 and not 2, and there is no function on the field that would return 2 without trimming it.
A comparison pads the shorter side
The three tests are the way COBOL compares text: the shorter operand is padded on the right with spaces until the two have the same length, and then the bytes are compared one by one. The literal "AB", the PIC X(2) field and the PIC X(4) field that already holds two spaces are therefore all equal to the four-byte field.
This is the reason trailing spaces rarely cause trouble in a IF and always cause trouble in a report: the comparison forgives them, the printer does not. It is also the reason a key built by STRING and a key built by a MOVE can differ in bytes and still match in a comparison, which is convenient until a file is indexed by the bytes.
Text against a number
When one side of a comparison is numeric, the comparison is numeric: the text is converted to a number first. A code field that holds "007" is equal to the number 7 and not equal to the text "7":
IDENTIFICATION DIVISION.
PROGRAM-ID. MIXED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 T-FLD PIC X(3) VALUE "007".
01 N-FLD PIC 9(3) VALUE 7.
01 S-FLD PIC X(4) VALUE "AB".
01 W-FLD PIC X(6) VALUE SPACES.
01 N-2 PIC X(2) VALUE SPACES.
PROCEDURE DIVISION.
IF T-FLD = N-FLD
DISPLAY "text 007 equals number 7"
ELSE
DISPLAY "text 007 is not number 7"
END-IF.
IF T-FLD = "7"
DISPLAY "text 007 equals text 7"
ELSE
DISPLAY "text 007 is not text 7"
END-IF.
MOVE S-FLD TO W-FLD.
DISPLAY "moved [" W-FLD "] len " LENGTH OF W-FLD.
MOVE S-FLD TO N-2.
DISPLAY "narrow [" N-2 "]".
STOP RUN.
cobc -x -free -Wall -o mixed mixed.cob
./mixed
text 007 equals number 7
text 007 is not text 7
moved [AB ] len 6
narrow [AB]
The first line converts the three characters into the number seven and compares numbers, so the leading zeros do not matter. The second line compares text with text, pads "7" to "7 ", and the two strings differ at the second byte. A program that looks up a code from a file and compares it with a literal is on the second line, and the literal has to have the leading zeros or the field has to be trimmed.
Moving text of another width
The two moves at the end of the program are the rules of MOVE seen on text: into a wider field the value is padded on the right, and into a narrower one it is cut on the right. "AB" in six bytes is AB and four spaces; the same field moved into two bytes is AB and nothing else.
A value that was cut is not recoverable, and the move that cuts it does not warn when the source is alphanumeric. The build warning value size exceeds data size is for a literal, not for a field, so a MOVE of a long text field into a short one is silent.
A blank field
A field of spaces is a value like any other, and it is not zero:
IDENTIFICATION DIVISION.
PROGRAM-ID. SPACES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME-F PIC X(8) VALUE "ADA".
01 PART-F PIC X(3) VALUE "ADA".
01 ALLSP-F PIC X(8) VALUE SPACES.
PROCEDURE DIVISION.
DISPLAY "name [" NAME-F "] part [" PART-F "]".
IF NAME-F = PART-F
DISPLAY "name equals part"
ELSE
DISPLAY "name is not part"
END-IF.
IF ALLSP-F = SPACES
DISPLAY "blank field equals SPACES"
END-IF.
IF ALLSP-F = ZERO
DISPLAY "blank field equals ZERO"
ELSE
DISPLAY "blank field is not ZERO"
END-IF.
STOP RUN.
cobc -x -free -Wall -o spaces spaces.cob
./spaces
name [ADA ] part [ADA]
name equals part
blank field equals SPACES
blank field is not ZERO
The blank test is the one to remember. A field that has never been written is spaces, a comparison with SPACES succeeds, and a comparison with ZERO fails; a numeric field that has never been written is zeros, and the two tests give the opposite answers. The lesson on VALUE and the initial state has the bytes, and the next lesson counts characters inside a line, spaces included.
Exercises
- Write a comparison of a PIC X(5) field holding "AB" with a PIC X(3) field holding "AB ". Is it true, and why?
- Compare "007" in a text field with "07" and with 7. Which comparison is true?
- Move a six-character word into a four-character field and print both fields. What happened to the last two characters?
- Print the length of a field after moving SPACES into it and after moving ZERO. Are the two lengths different?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does LENGTH OF return for a
PIC X(8)field that holds "ADA"?Answer
8: the width of the picture
A COBOL text field has no terminator and no separate length; the picture is the length.
-
How does COBOL compare a
PIC X(4)field holding "AB " with the literal "AB"?Answer
It pads the shorter operand with spaces, so they are equal
Alphanumeric comparison pads the shorter operand on the right until both have the same length.
-
What happens when a text field is compared with a numeric field?
Answer
The comparison is numeric: the text is converted to a number
A numeric operand makes the comparison numeric, so "007" equals 7 while the same text does not equal the text "7".
-
What is in a text field that has never been written?
Answer
Spaces
An alphanumeric field starts as spaces, which is why a test against SPACES succeeds and a test against ZERO fails.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-FLD PIC X(4) VALUE "AB". PROCEDURE DIVISION. DISPLAY "[" A-FLD "] len " LENGTH OF A-FLD. STOP RUN.Answer
[AB ] len 4
The value is padded on the right to the four bytes of the picture, and the length is the picture.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-FLD PIC X(3) VALUE "007". 01 N-FLD PIC 9(3) VALUE 7. PROCEDURE DIVISION. IF T-FLD = N-FLD DISPLAY "equal" ELSE DISPLAY "not equal" END-IF. STOP RUN.Answer
equal
One side is numeric, so the characters 007 are converted to the number seven and the two numbers match.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-FLD PIC X(3) VALUE "007". PROCEDURE DIVISION. IF T-FLD = "7" DISPLAY "equal" ELSE DISPLAY "not equal" END-IF. STOP RUN.Answer
not equal
Both sides are text, so "7" is padded to "7 " and differs from "007" at the second byte.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 S-FLD PIC X(4) VALUE "AB". 01 N-2 PIC X(2) VALUE SPACES. PROCEDURE DIVISION. MOVE S-FLD TO N-2. DISPLAY "[" N-2 "]". STOP RUN.Answer
[AB]
A move into a narrower alphanumeric field keeps the left end, so the two spaces of the source do not fit.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 ALLSP-F PIC X(8) VALUE SPACES. PROCEDURE DIVISION. IF ALLSP-F = ZERO DISPLAY "equals ZERO" ELSE DISPLAY "not ZERO" END-IF. STOP RUN.Answer
not ZERO
The field holds spaces, and a numeric comparison of spaces is not zero, so the else branch runs.
-
A program reads a code from a file into
PIC X(3)and compares it with "7". The file holds "007". What does the test do?Answer
It reports not equal, because the two texts differ after padding
Both operands are text, so the comparison is character by character and the leading zeros of the file value do not match.
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