COBOL MOVE

MOVE statement is used to copy the literal or value of a data item(variable) to other data item(variable) in COBOL.

Important:
MOVE statement does not mean moving the data from one variable to another variable. MOVE statement copies the value of a variable to another variable. The value of the source variable does not get changed.

There are various types of MOVE statements-
  1. SIMPLE MOVE​
  2. GROUP VARIABLE MOVE​
  3. CORRESPONDING MOVE​
  4. REFERENCE MODIFICATION MOVE​

1. Simple MOVE

1.1 Simple MOVE Syntax/format 1:

MOVE “HELLO WORLD”  TO WS-VARIABLE
Here, WS-VARIABLE will contain a value of ‘HELLO-WORLD’

MOVE 123 TO WS-NUM-VARIABLE.
Here, WS-NUM-VARIABLE will contain a value of 123

MOVE X TO Y
Here, the value of variable X will be moved the Y 

1.2 Simple MOVE Syntax/format 2:

MOVE GRP-1-VAR TO GRP-2-VAR
Here, the value of the entire group i.e. GRP-1-VAR is copied to other groups variable GRP-2-VAR

2. CORRESPONDING MOVE

The CORRESPONDING MOVE is used mostly in those scenarios when there are more than one data items with the same name which is present in more than one group variable.

For Example,

If DATA-ITEM-1 is present under GROUP-1 and GROUP-2 as well. If we want to copy the value of DATA-ITEM-1 which is under GROUP-1 to DATA-ITEM-1 which is present under GROUP-2, we can do a CORRESPONDING MOVE in this CASE.

2.1 CORRESPONDING MOVE Syntax/format:

MOVE-CORRESPONDING GRP-1-VAR TO GRP-2-VAR
Here, the value/s of matching data items from GRP-1-VAR will be copied to the matching data items of GRP-2-VAR

Reference modification is used to

copy the part of a string to another string or variable. This is same as SUBSTRING MOVE i.e. moving a substring to another string.

To perform Reference modification, we have to provide 2 things-

a) Starting position of the source string from where the string needs to be copied.

b) Length of the string which needs to be copied.

For Example,

If DATA-ITEM-1 has a value of “HELLO WORLD” and we want to copy only ‘WORLD’ from this String to other string
DATA- ITEM-2, we can simply give –

MOVE DATA-ITEM-1(6,5) TO DATA-ITEM-2.
Here, 6 is the starting position and 5 is the length which needs to be copied from the starting position

3. REFERENCE MODIFICATION MOVE

3.1 REFERENCE MODIFICATION MOVE Syntax/format:

MOVE-CORRESPONDING VAR-1(STRT-POSITION:LENGTH) TO VAR-2

TutorialBrain-Different types of MOVE Statements-Part 1
TutorialBrain-Different types of MOVE Statements-Part 2
TutorialBrain-Different types of MOVE Statements-Part 3
This picture describes the different forms of a MOVE statement

An Output of the different types of MOVE statement

TutorialBrain-Output of Different types of MOVE Statements
This picture describes the different forms of a MOVE statement

Tutorials for all brains!