Cobol and Mainframe

The Fundamental of the Cobol Redefine

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.

Syntax Details:

  • 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

Example 1:

IDENTIFICATION DIVISION.
PROGRAM-ID. redefine.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-REDEFINES-EX.
    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).
PROCEDURE DIVISION.
A000-MAIN-SECTION.
DISPLAY "WS-TODAYS-DATE: "WS-TODAYS-DATE.
DISPLAY "WS-CURR-DTE: "WS-CURR-DTE.
STOP RUN.

Output:

papan@papan-VirtualBox:~ /Desktop/pp/new$ cobc -xjF one.cbl

one.cbl: 11: warning: numeric value is expected

WS-TODAYS-DATE: 2020717

WS-CURR-DTE: 2020717

papan@papan-VirtualBox:~ /Desktop/pp/new$

Explanation:

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.

Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. EMPLOYEE.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 EMP-REC.
    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).
PROCEDURE DIVISION.
A000-MAIN-SECTION.
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.
STOP RUN.

Output:

papan@papan-VirtualBox:~ /Desktop/pp/new$ cobc -xjF two.cbl

EMP-REC-LY: DAVID MARTINEZ GOMES

EMP-NAME-DTL: DAVID MARTINEZ GOMES

EMP-FNAME: DAVID MARTINEZ

EMP-LNAME: GOMES

EMP-DPT-NAME : DAVID MARTINEZ GOMES

EMP-ADD : INEZ GOMES

Explanation:

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).

Example 3:

IDENTIFICATION DIVISION.
PROGRAM-ID. MONTHS_TABLE.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 MONTH-TBL.
    05 MNTH-NME.
        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).
PROCEDURE DIVISION.
DISPLAY "MONTH-TBL: "MONTH-TBL.
DISPLAY "MONTH-NME: " MNTH-NME.
STOP RUN.

Output:

papan@papan-VirtualBox:~/Desktop/pp/new$ cobc -xjF three.cbl

MONTH-TBL: JANFEBMAR

MNTH-NME: JANFEBMAR

papan@papan-VirtualBox:~/Desktop/pp/new$

Explanation: 

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.

Conclusion

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.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com