Wednesday, February 17, 2010

4. MOVE statements

A value can be moved from one storage location to another by the move statement. Contents of receiving field are changed after the operation.


Verb

Sending field
Receiving field



MOVE AMT-IN TO AMT-OUT.

The contents of AMT-In will be copied to the second field, AMT-OUT, as a result of the MOVE operation.

Eg 4.1:
MOVE TOTAL TO PRINT-TOTAL
MOVE ‘INVALID’ TO MESSAGE.
MOVE ZEROS TO TOTAL




Types of MOVE :
The MOVE statement can be categorized based on the receiving field : numeric MOVEs and non-numeric MOVEs.

Numeric MOVE
A numeric move is one in which a numeric field or literal is moved to a numeric receiving field.

When Sending and Receiving fields have the same PIC clauses
If the PIC clauses of both fields are identical, the contents of identifier-2 will be replaced with the contents of identifier-1 and the sending field will be unchanged.

When Sending and Receiving fields have different PIC clauses
Rule 1 : Moving integer portions of numeric fields
When moving an integer sending field or an integer portion of a numeric sending field to a numeric receiving field, move, movement is from right to left. All nonfilled high-order (leftmost) integer positions of the receiving field are replaced with zeros.

Eg 4.2:
05 AMT-IN PIC 999 VALUE 123.
05 AMT-OUT PIC 9(4) VALUE 4567.
MOVE AMT-IN TO AMT-OUT.
Result :
AMT-OUT = 0123

Avoiding truncation
In a numeric move, if the receiving field has fewer integer positions than the sending the field, the most significant digits will be truncated.

Eg 4.3:
05 AMT-IN PIC 999 VALUE 123.
05 AMT-OUT PIC 9(2) VALUE 45.

MOVE AMT-IN TO AMT-OUT.

Result :
AMT-OUT = 23

Rule 2: Moving decimal portions of numeric fields
When moving a decimal portion of a numeric sending field to the decimal portion of a numeric receiving field, movement is from left to right, beginning at the implied decimal point. Low-order (rightmost) non-filled decimal portions of the receiving field are replaced with zeros.

Eg 4.4:
a. When receiving field has more decimal positions than the sending field
05 AMT-IN PIC 99V99 VALUE 12.34.
05 AMT-OUT PIC 99V999 VALUE 56.789.

MOVE AMT-IN TO AMT-OUT.

Result :
AMT-OUT = 12.340

b. When receiving field has fewer decimal positions than the sending field
05 AMT-IN PIC V99 VALUE.34.
05 AMT-OUT PIC V9 VALUE .5.

MOVE AMT-IN TO AMT-OUT.

Result :
AMT-OUT = .3

Note : The same rules apply to numeric literals moved to numeric fields

Non-numeric MOVE
A non-numeric MOVE operation occurs in the following cases :
1. Moving an alphanumeric or alphabetic field, defined by a PICTURE of X’s or A’s, to another alphanumeric or alphabetic field.
2. Moving a non-numeric literal to an alphanumeric or alphabetic field.
3. Moving a numeric field or numeric literal to an alphanumeric field or to any group item.

Rule :
In a non-numeric move, data is transmitted from the sending field to the receiving field from left to right. Low-order or rightmost positions of the receiving field that are not replaced with sending field characters are filled with spaces.

Eg 4.4:
a. When receiving field is larger than the sending field

05 NAME-IN PIC XXX VALUE “ABC”.
05 NAME-OUT PIC X(5) VALUE “DEFGH”.

MOVE NAME-IN TO NAME-OUT.

Result :
NAME-OUT = ABCbb

b. When receiving field is smaller than the sending field

05 NAME-IN PIC XXX VALUE “ABC”.
05 NAME-OUT PIC XX VALUE “PQ”.

MOVE NAME-IN TO NAME-OUT.

Result :
NAME-OUT = AB

c. When the sending field is numeric integer and the receiving field is non-numeric

05 NAME-IN PIC 999 VALUE 321
05 NAME-OUT PIC X(5) VALUE “DEFGH”.

MOVE NAME-IN TO NAME-OUT.

Result :
NAME-OUT = 321bb

d. When the sending field is a non-numeric literal

05 NAME-OUT PIC X(5) VALUE “DEFGH”.

MOVE “XYZ” TO NAME-OUT.

Result :
NAME-OUT = XYZbb

e. When the sending field is a figurative constant

05 NAME-OUT PIC X(5) VALUE “DEFGH”.

MOVE SPACES TO NAME-OUT.

Result :
NAME-OUT = bbbbb

A group move is considered a non-numeric move
All group items, even those with numeric subfields, are treated as alphanumeric fields.

Eg 4.5:
05 DATE-OUT.
10 MONTH-OUT PIC 99 .
10 YEAR-OUT PIC 99.

a. MOVE 1 TO MONTH-OUT.MOVE 99 TO YEAR-OUT.

Result :
DATE-OUT = 0194

b. MOVE 194 TO DATE-OUT.

Result :
DATE-OUT = 194b

Permissible MOVE operations

Sending Field
Receiving Field
Numeric
Alphabetic
Alphanumeric
Group item
Numeric

* Numeric integer fields can be moved to alphanumeric fields but numeric fields with a V in the PIC clause cannot be moved to alphanumeric fields.


The MOVE CORRESPONDING statement
In the MOVE CORRESPONDING statement, all elementary items within the sending group-item that have the same names as corresponding elementary items in the receiving group-item will be moved. The same-named fields in the receiving group-item need not be in any specific order. Any fields of the sending record, that are not matched by the same-named fields in the receiving record are ignored.

Back to COBOL Index

No comments: