In this article, we will try to understand the COBOL REDEFINE. This is used to explain the same field storage into many ways.
It means that the definition and the redefinition of the field both refer to the same bytes of storage. COBOL REDEFINES cannot have a value clause and it cannot be used at level 1 in the FILE or REPORT section.
The Fundamental of REDEFINE Clause
You write a program to generate a report and you know that report has three different sections. The first section is the header, the second section is the detail record, and the third section is the trailer. There are two ways which you can define the structure in your program. The first one is to define each and every section in your file division section or may be in your work storage section. Another option is you can define the 132 bytes for your header record. Similarly, you can redefine the detail record and the trailer record again.
In short, you can use the REDEFINE clause to refer to the same memory location. If you want to write in the header, move the records to the particular header files and you can write that report. You are referring to the same memory location to the same bytes of storage which is 132 bytes.
- The redefine clause must follow immediately after the data-name-2.
- The data-name-1 cannot have the value clause.
- The level-number of the data-name-1 = data-name -2 must not be 66 or 88.
- The data-name-1 size should not exceed data-name-2 size. However, it can be less than the data-name-2.
- The data-name-2 item cannot contain an OCCURS clause.
- The 01-49 data-name-1.
- REDEFINES the data-name-2.
Some Examples of COBOL REDEFINE
05 WS-TODAYS-DATE PIC 9(08) VALUE "2020717".
05 WS-CURR-DTE REDEFINES WS-TODAYS-DATE.
10 CC PIC 9(02).
10 YY PIC 9(02).
10 MM PIC 9(02).
10 DD PIC 9(02).
DISPLAY "WS-TODAYS-DATE: "WS-TODAYS-DATE.
DISPLAY "WS-CURR-DTE: "WS-CURR-DTE.
one.cbl: 11: warning: numeric value is expected
In this example, we redefine today’s date into the current date which is actually a group variable of century, year, month, and day. Actually, we split today’s date into four different fields. So, the first line is to define the level number 05 which is a variable called today’s date and the size of the variable is 9 of 8. Now, the current date is the new variable which is actually the redefinition of today’s date and group of four fields. The combined size of all fields is 8 bytes.
05 EMP-REC-LY PIC X(30) VALUE "DAVID MARTINEZ GOMES".
05 EMP-NAME-DTL REDEFINES EMP-REC-LY.
10 EMP-FNAME PIC X(15).
10 EMP-LNAME PIC X(15).
05 EMP-DPT-NAME REDEFINES EMP-REC-LY.
10 EMP-DPT PIC X(10).
10 EMP-ADD PIC X(15).
DISPLAY "EMP-REC-LY: "EMP-REC-LY.
DISPLAY "EMP-NAME-DTL: " EMP-NAME-DTL.
DISPLAY "EMP-FNAME: " EMP-FNAME.
DISPLAY "EMP-LNAME:" EMP-LNAME.
DISPLAY "EMP-DPT-NAME:" EMP-DPT-NAME.
DISPLAY "EMP-ADD:" EMP-ADD.
EMP-REC-LY: DAVID MARTINEZ GOMES
EMP-NAME-DTL: DAVID MARTINEZ GOMES
EMP-FNAME: DAVID MARTINEZ
EMP-DPT-NAME : DAVID MARTINEZ GOMES
EMP-ADD : INEZ GOMES
In this example, we redefined the same variable multiple times. There exists two fields: EMP-REC-LY which is the emp detail record and EMP-DPT-NME which is the emp department name. These are two variables which actually redefined EMP-REC-LY which is x (30). If you see the first one which is EMP-NAME-DTL, it is again a group variable of the first and last names with the size of 15 and 10, respectively. Similarly, we just look at the second redefinition which is EMP-DPT-NME which is again a redefinition of EMP-REC-LY. It is a group of variable x (10) and x (15).
10 FILLER PIC X(03) VALUE 'JAN'.
10 FILLER PIC X(03) VALUE 'FEB'.
10 FILLER PIC X(03) VALUE 'MAR'.
05 FILLER REDEFINES MNTH-NME.
10 MONTH-NME OCCURS 3 TIMES.
15 MNTH PIC X(03).
DISPLAY "MONTH-TBL: "MONTH-TBL.
DISPLAY "MONTH-NME: " MNTH-NME.
Let’s move on to this example where we used the REDEFINE clause to initialize the month name. If you print the value of month name, table, or array, you will find that the first occurrence goes to have “JAN”, the second occurrence have “FEB”, and the third occurrence have “MAR”. With this technique, you can actually assign initial values to your array or table.
In this article, we learned about the general syntax of the “REDEFINE” statement in COBOL and the fundamental concept of this statement where we can actually use this particular statement is explained. We hope that you understand the application of this statement.