COBOL Redefines

Difference between Search and Search ALL

Advertisements


The REDEFINES clause allows developer to use already declared for data description entries computer storage area for different data description entries.


Syntax:

level-number data-name-1/FILLER REDEFINES data-name-2.

In the above syntax, data-name-1/FILLER is the REDEFINING data item.

Data-item-2 is REDEFINED data item.

REDEFINES will not change any data in the REDEFINED item and will simple re-use the data.


Rules:

REDEFINED data item can be redefined any number of times.


Ex:
	01 A PIC X(10).
	01 B REDEFINES A PIC X(10).
	01 C REDEFINES A PIC X(09).

But REDEFINING item can’t be redefined again. In the above example, B/C can’t be redefined.

The redefining level numbers should be same.

Redefines clause can be used with level numbers 01 to 49 and 77.

The level numbers 66, 88 can’t be redefined.

The fields can be redefined only which have same group, same level numbers and immediately follow the field.

REDEFINES can have a picture clause.

REDEFINES a data item can be declared irrespective of data types which are already declared.

REDEFINING field length may be is equal, less than or greater than the redefined field.

REDEFINING field should be coded with the same level number, same group.

REDEFINING data item can contain elementary data items during the REDEFINES.


Ex:
	01 A PIC X(10).
	01 B REDEFINES A.
		02  B1 PIC X(5).
		02  B2 PIC X(5).

Data items can be redefined without changing the length of existing data item.


Ex:
	01 A PIC X(10).
	01 B REDEFINES A PIC X(10).

Data items can be redefined by changing the length of existing data item.


Ex:
	01 A PIC X(10).
	01 B REDEFINES A PIC X(10).
	01 C REDEFINES A PIC X(09).

Data items can be redefined by changing the usage of existing data item.


Ex:
	01 A PIC 9(10) USAGE DISPLAY.
	01 B REDEFINES A PIC 9(05) USAGE COMP-3.

Example:

Ex:
	01 A PIC X(10).
	01 B REDEFINES A PIC X(10).
	01 C REDEFINES A PIC X(09).

In the above example, A declared as alpha-numeric item of length 10. Assume that the memory locations allocated for A starts from 0 to 10.

B REDEFINES A of length 10. So B also points the same memory location from starting byte and represents the same data which is in A.

C REDEFINES A of length 9. So C also points the same memory location from starting byte and represents the same data which is in A but only first only 9 bytes.


Diagram:



Practical Example:


Below example describes about how the REDEFINES useful.


Code:


IDENTIFICATION DIVISION.                                        
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
 01 WS-DATA-A       PIC X(20) VALUE 'MAINFRAMESTECHHELP'. 
 01 WS-DATA-B       REDEFINES WS-DATA-A PIC X(20).

PROCEDURE DIVISION.                                             
    
    DISPLAY 'ACTUAL DATA IN WS-DATA-A : ' WS-DATA-A.
    DISPLAY 'ACTUAL DATA IN WS-DATA-B : ' WS-DATA-B.

    MOVE 'THE MAINFRAMESTECHHELP' TO WS-DATA-B.

    DISPLAY 'ACTUAL DATA AFTER MOVE IN WS-DATA-A : ' WS-DATA-A.
    DISPLAY 'ACTUAL DATA AFTER MOVE IN WS-DATA-B : ' WS-DATA-B.

    STOP RUN.                                          
	   

If any one of the data item modified in the redefined data items, second one automatically reflected the same.


Output:


ACTUAL DATA IN WS-DATA-A : MAINFRAMESTECHHELP
ACTUAL DATA IN WS-DATA-B : MAINFRAMESTECHHELP

ACTUAL DATA AFTER MOVE IN WS-DATA-A : THE MAINFRAMESTECHHELP
ACTUAL DATA AFTER MOVE IN WS-DATA-B : THE MAINFRAMESTECHHELP



Difference between Search and Search ALL

Advertisements