REDEFINES
Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 9 of 19.
REDEFINES gives the same storage a second description:
01 DATE-IN PIC 9(8).
01 DATE-PARTS REDEFINES DATE-IN.
05 D-YEAR PIC 9(4).
05 D-MONTH PIC 99.
05 D-DAY PIC 99.A date read into DATE-IN as 20261123 is also D-YEAR 2026, D-MONTH 11 and D-DAY 23. Nothing is copied: changing D-MONTH changes DATE-IN.
The redefining item comes right after the one it redefines, at the same level number, and carries no VALUE.
Tables with starting values are built this way: FILLER items hold the values, and a table redefines them.
01 DAY-DATA.
05 FILLER PIC X(12) VALUE "MonTueWedThu".
05 FILLER PIC X(9) VALUE "FriSatSun".
01 DAY-TABLE REDEFINES DAY-DATA.
05 DAY-NAME PIC X(3) OCCURS 7 TIMES.Each view reads the bytes its own way. Letters seen through a numeric view are not a number, and arithmetic on them stops the program, the way an S0C7 ends a mainframe job.
Challenge
EasyThe program reads a date as 8 digits, YYYYMMDD, into DATE-IN. Declare DATE-PARTS redefining it as D-YEAR, D-MONTH and D-DAY, so it prints 23/11/2026.
Example input, one line per ACCEPT: 20261123.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. DATEFMT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DATE-IN PIC 9(8).
*> Declare DATE-PARTS here
PROCEDURE DIVISION.
ACCEPT DATE-IN.
DISPLAY D-DAY "/" D-MONTH "/" D-YEAR.
STOP RUN.