Moving a Group
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 4 of 23.
A MOVE from one group to another copies bytes from left to right, as with PIC X fields. Nothing is converted item by item. When two records have the same layout, one MOVE copies every item:
MOVE NEW-CUST TO LAST-CUST.The rules of text apply to the whole group: a shorter sender is padded with spaces on the right, and a longer one is cut on the right.
Spaces in a numeric item are not a number. After MOVE SPACES to a record, adding to its numeric item stops the program with a data exception:
01 ORDER-REC.
05 ORD-ITEM PIC X(6).
05 ORD-QTY PIC 9(3).
MOVE SPACES TO ORDER-REC.
ADD 1 TO ORD-QTY. *> stops: not numericINITIALIZE clears a record item by item: text items become spaces and numeric items become zeros.
INITIALIZE ORDER-REC.
ADD 1 TO ORD-QTY. *> 001Challenge
EasyThe program reads a customer into NEW-CUST. Copy it into LAST-CUST, which has the same layout, with one MOVE, so the program prints the name and city from LAST-CUST.
Example input, one line per ACCEPT: Ana, Lisbon.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. CUSTS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NEW-CUST.
05 NEW-NAME PIC X(10).
05 NEW-CITY PIC X(10).
01 LAST-CUST.
05 LAST-NAME PIC X(10).
05 LAST-CITY PIC X(10).
PROCEDURE DIVISION.
ACCEPT NEW-NAME.
ACCEPT NEW-CITY.
*> Copy NEW-CUST into LAST-CUST here
DISPLAY LAST-NAME.
DISPLAY LAST-CITY.
STOP RUN.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.