Redefining Variable with Same Length Example


Scenario - Redefining Variable with Same Length

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REDEFSAM.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-VAR1                       PIC X(20). 
      * Redefining variable with the same length
          05 WS-REQ-VAR1 REDEFINES WS-VAR1 PIC X(20). 

       PROCEDURE DIVISION.
           MOVE "MAINFRAME TECHNOLOGY"     TO WS-VAR1.
           DISPLAY "WS-VAR1:       " WS-VAR1. 
           DISPLAY "WS-REQ-VAR1:   " WS-REQ-VAR1. 
           DISPLAY " ". 

           MOVE "MAINFRAME APPLICATION SYSTEM" TO WS-REQ-VAR1.
           DISPLAY "WS-VAR1:       " WS-VAR1.
           DISPLAY "WS-REQ-VAR1:   " WS-REQ-VAR1.

           STOP RUN.

Output -

WS-VAR1:       MAINFRAME TECHNOLOGY
WS-REQ-VAR1:   MAINFRAME TECHNOLOGY 

WS-VAR1:       MAINFRAME APPLICATIO
WS-REQ-VAR1:   MAINFRAME APPLICATIO 

Explaining Example -

In the above example:

  • WS-VAR1 is the source variable. WS-REQ-VAR1 is the target variables on WS-VAR1.
  • WS-REQ-VAR1 redefines WS-VAR1 with the same length of 20. So, WS-RLE-VAR1 and WS-VAR1 can have the same data.
  • Initially, WS-VAR1 was assigned with "MAINFRAME TECHNOLOGY" and the redefined variable WS-REQ-VAR1 also had the same data.
  • Later, the "MAINFRAME APPLICATION SYSTEM" was assigned to WS-REQ-VAR1. WS-VAR1 can access the same memory location of WS-VAR1. So it also has the same data.