MOVE: numeric, alphanumeric and groups
MOVE is the most used statement in COBOL and the one with the most rules. It has three of them: a numeric move converts by value, an alphanumeric move copies characters, and a group move copies bytes. Which one applies is decided by the receiver and the sender together, and the compiler says which when it can.
Numeric moves: value, not bytes
When both sides are numeric, the move is a conversion: the value is placed in the receiving picture with its implied point at the right place, the free digits are filled with zeros and the digits that do not fit are cut.
IDENTIFICATION DIVISION.
PROGRAM-ID. NUMS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-SHORT PIC 9(3).
01 N-DEC PIC 9(3)V99.
01 N-ONE PIC 9(3)V9.
PROCEDURE DIVISION.
MOVE 7 TO N-SHORT.
DISPLAY "7 [" N-SHORT "]".
MOVE -7 TO N-SHORT.
DISPLAY "-7 [" N-SHORT "]".
MOVE 5.5 TO N-DEC.
DISPLAY "5.5 [" N-DEC "]".
MOVE 12.345 TO N-ONE.
DISPLAY "12.345 [" N-ONE "]".
STOP RUN.
cobc -x -free -Wall -o nums nums.cob
./nums
nums.cob:13: warning: ignoring sign [-Wtruncate]
11 | MOVE 7 TO N-SHORT.
12 | DISPLAY "7 [" N-SHORT "]".
13 > MOVE -7 TO N-SHORT.
14 | DISPLAY "-7 [" N-SHORT "]".
15 | MOVE 5.5 TO N-DEC.
nums.cob:17: warning: value size exceeds data size [-Wtruncate]
15 | MOVE 5.5 TO N-DEC.
16 | DISPLAY "5.5 [" N-DEC "]".
17 > MOVE 12.345 TO N-ONE.
18 | DISPLAY "12.345 [" N-ONE "]".
19 | STOP RUN.
nums.cob:17: note: value is 12345 [-Wtruncate]
nums.cob:8: note: 'N-ONE' defined here as PIC 9(3)V9 [-Wtruncate]
6 | 01 N-SHORT PIC 9(3).
7 | 01 N-DEC PIC 9(3)V99.
8 > 01 N-ONE PIC 9(3)V9.
9 |
10 | PROCEDURE DIVISION.
7 [007]
-7 [007]
5.5 [005.50]
12.345 [012.3]
The second line is the sign rule: the picture has no S, so the sign has no place to go and is dropped with a warning. The third line shows why the move is a conversion and not a copy: 5.5 becomes 005.50, the point of the receiver sitting where both hundredths are, and the value is unchanged. The fourth line is the other direction: a third decimal place has nowhere to go in PIC 9(3)V9, so it is cut and the value is wrong by five thousandths.
Alphanumeric moves: left, pad, cut
When the receiver is alphanumeric, the move is a copy of characters: they start at the leftmost position, the free positions on the right become spaces, and a sender longer than the receiver is cut on the right.
IDENTIFICATION DIVISION.
PROGRAM-ID. ALPHA.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-SHORT PIC X(3).
01 A-WIDE PIC X(6).
PROCEDURE DIVISION.
MOVE "AB" TO A-WIDE.
DISPLAY "AB into X(6) [" A-WIDE "]".
MOVE "ABCDEF" TO A-SHORT.
DISPLAY "ABCDEF into X(3) [" A-SHORT "]".
MOVE 42 TO A-WIDE.
DISPLAY "42 into X(6) [" A-WIDE "]".
STOP RUN.
cobc -x -free -Wall -o alpha alpha.cob
./alpha
alpha.cob:12: warning: value size exceeds data size [-Wtruncate]
10 | MOVE "AB" TO A-WIDE.
11 | DISPLAY "AB into X(6) [" A-WIDE "]".
12 > MOVE "ABCDEF" TO A-SHORT.
13 | DISPLAY "ABCDEF into X(3) [" A-SHORT "]".
14 | MOVE 42 TO A-WIDE.
alpha.cob:12: note: value size is 6 [-Wtruncate]
alpha.cob:6: note: 'A-SHORT' defined here as PIC X(3) [-Wtruncate]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 A-SHORT PIC X(3).
7 | 01 A-WIDE PIC X(6).
8 |
alpha.cob:14: warning: alphanumeric value is expected [-Wtyping]
12 | MOVE "ABCDEF" TO A-SHORT.
13 | DISPLAY "ABCDEF into X(3) [" A-SHORT "]".
14 > MOVE 42 TO A-WIDE.
15 | DISPLAY "42 into X(6) [" A-WIDE "]".
16 | STOP RUN.
alpha.cob:7: note: 'A-WIDE' defined here as PIC X(6) [-Wtyping]
5 | WORKING-STORAGE SECTION.
6 | 01 A-SHORT PIC X(3).
7 > 01 A-WIDE PIC X(6).
8 |
9 | PROCEDURE DIVISION.
AB into X(6) [AB ]
ABCDEF into X(3) [ABC]
42 into X(6) [42 ]
The padding and the cut are the mirror of the numeric rules, and the asymmetry is the point: a number grows on the left, a string on the right. The last line is the move a program should not write: a numeric sender into a PIC X receiver is accepted with alphanumeric value is expected, and the digits land at the left, which is where a text field wants them but not where a report column would.
Group moves: bytes without conversion
A group item is not a category, so a move into a group or out of one is a byte move: the bytes are copied from the start, with no padding, no conversion and no regard for where one child ends and the next begins.
IDENTIFICATION DIVISION.
PROGRAM-ID. GROUPS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FROM-REC.
05 F-A PIC X(3) VALUE "ABC".
05 F-B PIC 9(2) VALUE 42.
01 TO-REC.
05 T-A PIC X(3).
05 T-B PIC 9(2).
01 FLAT PIC X(5).
01 SHORT-FLAT PIC X(3).
PROCEDURE DIVISION.
MOVE FROM-REC TO TO-REC.
DISPLAY "group to group [" T-A "][" T-B "]".
MOVE FROM-REC TO FLAT.
DISPLAY "group to X(5) [" FLAT "]".
MOVE FROM-REC TO SHORT-FLAT.
DISPLAY "group to X(3) [" SHORT-FLAT "]".
STOP RUN.
cobc -x -free -Wall -o groups groups.cob
./groups
group to group [ABC][42]
group to X(5) [ABC42]
group to X(3) [ABC]
The first line is the reason a group move is useful: the record went across as a unit, and the two children on the other side have the same layout, so each of them is right. The second line shows what the record really is: five characters, ABC and the two digits of the number, with no separator between them. The third line shows the cut: only the first three bytes survive, so the number 42 is gone.
A group move between records of different layouts is the classic way to corrupt a file, because nothing is converted and nothing warns. A group move is right when the two descriptions are the same record, which is the case of a working record filled from a file record; it is wrong as soon as one side has a field the other does not.
MOVE CORRESPONDING
The statement for records that are almost the same is MOVE CORRESPONDING: it moves every elementary item whose name appears in both groups, at any depth, and leaves the rest alone. Order does not matter, size does not have to match, and each pair is moved by its own rules.
IDENTIFICATION DIVISION.
PROGRAM-ID. CORR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SRC.
05 CUST-ID PIC 9(3) VALUE 101.
05 CUST-NAME PIC X(6) VALUE "LONDON".
05 CUST-ADDR.
10 STREET-F PIC X(8) VALUE "MAIN ST".
01 DST.
05 CUST-NAME PIC X(6) VALUE "------".
05 CUST-ID PIC 9(3) VALUE 999.
05 CUST-ADDR.
10 STREET-F PIC X(8) VALUE "--------".
05 DST-ONLY PIC X(2) VALUE "YY".
PROCEDURE DIVISION.
MOVE CORRESPONDING SRC TO DST.
DISPLAY "name [" CUST-NAME IN DST "]".
DISPLAY "id [" CUST-ID IN DST "]".
DISPLAY "street [" STREET-F IN DST "]".
DISPLAY "dst-on [" DST-ONLY "]".
STOP RUN.
cobc -x -free -Wall -o corr corr.cob
./corr
name [LONDON]
id [101]
street [MAIN ST ]
dst-on [YY]
The name and the identifier were declared in the opposite order on the two sides and both arrived. The street is one level down inside CUST-ADDR and it arrived too, because CORRESPONDING looks at every level. The field the destination has and the source does not, DST-ONLY, was not touched, and that is the documented behaviour rather than an oversight: the statement only moves what it can pair.
The IN DST in every display line is not decoration. After the move there are two fields called CUST-NAME, one in each record, and a bare name is ambiguous; needs qualification. A program that copies records with CORRESPONDING qualifies the names on every read, which is a small price for not writing twenty moves.
The name that almost matches
CORRESPONDING pairs exact names and nothing else. A name that differs by one letter is a different name, the pair is not made, and the destination keeps whatever it had:
IDENTIFICATION DIVISION.
PROGRAM-ID. CORRTYPO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SRC.
05 CUST-NAME PIC X(6) VALUE "LONDON".
05 CUSTE-CODE PIC X(2) VALUE "AB".
01 DST.
05 CUST-NAME PIC X(6) VALUE "------".
05 CUST-CODE PIC X(2) VALUE "ZZ".
PROCEDURE DIVISION.
MOVE CORRESPONDING SRC TO DST.
DISPLAY "name [" CUST-NAME IN DST "] code [" CUST-CODE "]".
STOP RUN.
cobc -x -free -Wall -o corrtypo corrtypo.cob
./corrtypo
name [LONDON] code [ZZ]
The build is clean, the name moved, and the code is still ZZ. Nothing in the output of the compiler says that a field was left behind, because the statement did exactly what it says: it matched the names it found. The same silence covers a field that was renamed in one copy of a copybook, and the symptom is a value from the previous record in a report.
Two habits catch it. The first is to move the fields the program needs by name, one MOVE per field, when the two records differ at all. The second is to test the destination after the move: a field that should have arrived and is still the initial ZZ is visible in a test and invisible in a review. The lesson on REDEFINES comes next; a record that several programs share is kept in a copybook, which the course takes up in the part on files.
Exercises
- Move 1.005 into a PIC 9(2)V99 and print it. Is the value rounded or cut, and does the build say anything?
- Move " AB" into a PIC X(4) and print it between brackets. How many spaces are at the left, and what happened to them?
- Add a third child to FROM-REC and keep TO-REC as it is. Run the program and explain the output from the byte count.
- Add a field CUST-CITY to both records with different values, run the program with MOVE CORRESPONDING, and print it. Then remove one of the two and see what the build says.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a numeric MOVE convert?
Answer
The value, with the implied point aligned and zeros filling the free digits
A numeric move is a decimal conversion, so 5.5 into a two-decimal field becomes 005.50 and the value stays the same.
-
Where does an alphanumeric MOVE put the characters?
Answer
At the leftmost position, with spaces on the right
A character string starts at the first position, so "AB" in a wider field leaves spaces after it, not before.
-
What does a group MOVE copy?
Answer
The bytes, from the start, with no conversion
A group has no category, so the move is bytes; the children of the receiver decide what those bytes mean afterwards.
-
Which pairs does MOVE CORRESPONDING move?
Answer
Elementary items with the same name, at any depth, and nothing else
Pairing is by name, so order and level do not matter, and a name that is missing on one side is simply not moved.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-DEC PIC 9(3)V99. PROCEDURE DIVISION. MOVE 5.5 TO N-DEC. DISPLAY "[" N-DEC "]". STOP RUN.Answer
[005.50]
The receiver has two decimal places, so the value is scaled to them and printed in the full width with the implied point.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-ONE PIC 9(3)V9. PROCEDURE DIVISION. MOVE 12.345 TO N-ONE. DISPLAY "[" N-ONE "]". STOP RUN.Answer
[012.3]
One decimal place fits, the next is cut rather than rounded, so the field holds 012.3 and the build warns.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-SHORT PIC X(3). PROCEDURE DIVISION. MOVE "ABCDEF" TO A-SHORT. DISPLAY "[" A-SHORT "]". STOP RUN.Answer
[ABC]
An alphanumeric move keeps the left end and cuts the right, so the first three characters survive.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 FROM-REC. 05 F-A PIC X(3) VALUE "ABC". 05 F-B PIC 9(2) VALUE 42. 01 FLAT PIC X(5). PROCEDURE DIVISION. MOVE FROM-REC TO FLAT. DISPLAY "[" FLAT "]". STOP RUN.Answer
[ABC42]
A group move copies five bytes, the three letters and the two digits, with nothing added between them.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 SRC. 05 CUST-NAME PIC X(6) VALUE "LONDON". 05 CUSTE-CODE PIC X(2) VALUE "AB". 01 DST. 05 CUST-NAME PIC X(6) VALUE "------". 05 CUST-CODE PIC X(2) VALUE "ZZ". PROCEDURE DIVISION. MOVE CORRESPONDING SRC TO DST. DISPLAY "[" CUST-NAME IN DST "][" CUST-CODE "]". STOP RUN.Answer
[LONDON][ZZ]
CUSTE-CODE is not CUST-CODE, so the pair is never made and the destination keeps its own value without a warning.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-WIDE PIC X(6). PROCEDURE DIVISION. MOVE 42 TO A-WIDE. DISPLAY "[" A-WIDE "]". STOP RUN.Answer
[42 ]
The receiver is alphanumeric, so the digits go to the left and spaces fill the rest; the build warns that an alphanumeric value was expected.
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