MOVE Reference Modification
The reference modification is used for a special purpose to move part of data from the sending item to the receiving item. This is mainly useful when dealing with strings, where we might want to handle only a specific part of the data item.
variable (start-position [: length])
- variable - The name of the variable we're working with..
- start-position - The position in the variable where the substring begins. The first character is at position 1.
- length - Specifies the number of bytes from the starting position. If not provided, the substring includes all characters from the start-position to the end.
- The sending item can be an alphanumeric elementary item, a group item, or a literal or figurative constant.
- The receiving item should be an alphanumeric elementary item or group item but not a literal or figurative constant.
- MOVE statement can have one or more receiving items.
Practical Example -
Scenario - Formatting full phone number from three different sources.
----+----1----+----2----+----3----+----4----+----5----+ IDENTIFICATION DIVISION. PROGRAM-ID. MOVEREF. AUTHOR. MTH. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-COUNTRY-CODE PIC 9(02) VALUE 91. 01 WS-AREA-CODE PIC 9(03) VALUE 999. 01 WS-PHONE-NBR PIC 9(08) VALUE 87654321. 01 WS-FULL-PHN-NBR PIC 9(13). PROCEDURE DIVISION. MOVE WS-COUNTRY-CODE TO WS-FULL-PHN-NBR(1:2). MOVE WS-AREA-CODE TO WS-FULL-PHN-NBR(3:2). MOVE WS-PHONE-NBR TO WS-FULL-PHN-NBR(5:8). DISPLAY 'FULL PHONE NUMBER: ' WS-FULL-PHN-NBR. STOP RUN.
FULL PHONE NUMBER: 919987654321
Explaining Example -
In the above example:
- WS-COUNTRY-CODE value moved to first 2 bytes of WS-FULL-PHN-NBR.
- WS-AREA-CODE value moved to 3rd and 4th positions of WS-FULL-PHN-NBR.
- WS-PHONE-NBR value moved to WS-FULL-PHN-NBR from 5th position to 8 bytes.