Introducing the MOVE Verb
The MOVE verb in COBOL is a general-purpose verb, used to store a value in a variable. The general syntax for MOVE is the following:
MOVE value TO variable.
In this syntax, variable must be a variable defined in the DATA DIVISION, and value can be another variable or a constant.
Here are some examples:
MOVE 12 TO THE-NUMBER.
MOVE ONE-NUMBER TO ANOTHER-NUMBER.
MOVE "XYZ" TO THE-MESSAGE.
MOVE is used to set a variable to a specific value. For example, if you're going to use the variable THE-COUNTER as a counter and you need the count to start at 1, you might use the following as one method of setting up the variable with a starting value:
MOVE 1 TO THE-COUNTER.
MOVE in COBOL does not move memory physically from one place to another. It copies values from the source variable and stores them in the target variable. Table 2.2 describes the effect of some different MOVE examples that move constants and variables into variables. All variables are assumed to be defined in the WORKING-STORAGE SECTION.
Table 2.2. Examples of the MOVE verb.
Command | Effect |
MOVE 19 TO THE-NUMBER | Stores 19 in the variable THE-NUMBER, or sets THE-NUMBER to a value of 19 |
MOVE "Hello" TO THE-MESSAGE | Stores Hello in the variable THE-MESSAGE, or sets THE-MESSAGE to contain Hello |
MOVE A-NUMBER TO THE-NUMBER | Locates the variable named A-NUMBER, gets the value stored there, and copies it or moves it to the variable named THE-NUMBER |
MOVE THE-OLD-NAME TO THE-NEW-NAME | Locates the variable named THE-OLD-NAME, gets the value stored there, and copies it or moves it to the variable named THE-NEW-NAME |
Listing 2.7 is a program designed solely to provide examples of the MOVE verb. It combines PICTURE abbreviations, multiple DISPLAY statements, and MOVE statements to display two messages, with message numbers, on the screen. This will give you a further idea of the uses and effects of MOVE.
TYPE: Listing 2.7. Using MOVE.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLO03.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500
000600 WORKING-STORAGE SECTION.
000700
000800 01 THE-MESSAGE PIC X(20).
000900 01 THE-NAME PIC X(10).
001000 01 THE-NUMBER PIC 99.
001100
001200 PROCEDURE DIVISION.
001300
001400 PROGRAM-BEGIN.
001500
001600 DISPLAY "Enter someone's name.".
001700
001800 ACCEPT THE-NAME.
001900
002000 MOVE "Hello" TO THE-MESSAGE.
002100
002200 MOVE 1 TO THE-NUMBER.
002300
002400 DISPLAY "Message "
002500 THE-NUMBER
002600 ": "
002700 THE-MESSAGE
002800 THE-NAME.
002900
003000 MOVE "Say Goodnight," TO THE-MESSAGE.
003100
003200 MOVE 2 TO THE-NUMBER.
003300
003400 DISPLAY "Message "
003500 THE-NUMBER
003600 ": "
003700 THE-MESSAGE
003800 THE-NAME.
003900
004000
004100 PROGRAM-DONE.
004200 STOP RUN.
004300
OUTPUT:
C>pcobrun hello03
Enter someone's name.
Gracie
Message 01: Hello Gracie
Message 02: Say Goodnight, Gracie
C>
ANALYSIS: Lines 000800, 000900, and 001000 contain abbreviated PICTURES. THE-MESSAGE is a 20-character alphanumeric field, and THE-NAME is a 10-character alphanumeric field. The user is asked to enter a name, and this is accepted from the keyboard into THE-NAME at line 001800.
In lines 002000 and 002200, MOVE is used to move values to THE-MESSAGE and the THE-NUMBER. Lines 002400 through 002800 contain one long DISPLAY statement. Notice that this long statement ends with only one period, on line 002800. COBOL sentences can spread over more than one line as in this example, as long as they remain within Area B, which is columns 12 through 72. This DISPLAY creates one line of display information containing the values Message, THE-NUMBER, :, THE-MESSAGE, and THE-NAME, one after the other on a single line:
Message 01: Hello Charlie
Similar logic is repeated at lines 003000 through 003800, and a second line is displayed. See if you can guess how the output will appear before taking a look.
Note that the output from hello03.cbl is shown for an input name of Gracie. Listing 2.7 is a good program for practice. First type, edit, and compile hello03.cbl, and try it a couple of times. Then copy the program to hello04.cbl and edit it. Try different constants and display orders for the DISPLAY statements. Here are a couple of alternatives for the first DISPLAY statement:
DISPLAY "Line "
THE-NUMBER
"> "
THE-MESSAGE
THE-NAME.
DISPLAY THE-MESSAGE
THE-NAME
" was Number "
THE-NUMBER.
The following are sample output lines from these two formats:
Line 01> Hello Charlie
Hello Charlie was Number 01
One of the features of the MOVE verb is that it will pad a variable with spaces to the end if the value that is being moved into an alphanumeric field is too short to fill the field. This is convenient; it's almost always what you want. In the line MOVE "Hello" TO THE-MESSAGE, the first five characters of THE-MESSAGE are filled with Hello, and the remaining character positions are filled with spaces. (In Bonus Day 2, "Miscellaneous COBOL Syntax," you learn how to move a field while dropping the trailing space used for padding.)
MOVE pads numeric variables by filling them with zeroes to the left. Notice that a numeric value with a PIC 99 containing a value of 1 will display as 01 in Listing 2.7. This is because of the padding action of the MOVE verb.
Values that are too long are truncated. If THE-MESSAGE is defined as a PIC X(7), the line
MOVE "Hello world" to THE-MESSAGE
results in THE-MESSAGE containing Hello w and the rest of the value falling off the end.
Moving a value that is too large to a numeric variable results in a similar truncation, but on the left side. If THE-NUMBER is defined as a PIC 9999, the following line results in THE-NUMBER containing 1784:
MOVE 61784 TO THE-NUMBER
There isn't room for all five digits, so only the four digits on the right are picked up on the move.
Formatting Output
Listing 2.8 is another example of some of the principles you have learned. It displays three lines of a nursery rhyme involving some work that Jack had to do. The ADD verb (which is new in this listing) increments the value of THE-NUMBER as each line is displayed. In the DISPLAY statements, a space is used to separate the line number from the statement. Remember that the asterisk in column 7 is used to place a comment in the code.