>>SOURCE FORMAT IS FREE *> cobol-redefines-example.cob — one memory area, two views *> Dialect: GnuCOBOL 3.x, free format *> Status : desk-checked against the GnuCOBOL 3.2 Programmer's Guide; *> NOT compiled (no cobc on the authoring machine) *> Inputs : none *> Outputs: DISPLAY lines on the console *> Build : cobc -x cobol-redefines-example.cob *> Run : ./cobol-redefines-example (Windows: cobol-redefines-example.exe) IDENTIFICATION DIVISION. PROGRAM-ID. REDEFINES-DEMO. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. *> View 1: eight characters of text. View 2: three numbers in the *> very same eight bytes. Only the subject (Order-Date) has a VALUE. 01 Order-Date PIC X(8) VALUE "20260905". 01 Order-Date-Parts REDEFINES Order-Date. 05 OD-Year PIC 9(4). 05 OD-Month PIC 9(2). 05 OD-Day PIC 9(2). *> A record whose tail means different things depending on a type *> code. Both variants redefine TR-Body and are exactly 24 bytes. 01 Transaction-Record. 05 TR-Type PIC X. 88 TR-Is-Payment VALUE "P". 88 TR-Is-Refund VALUE "R". 05 TR-Body PIC X(24). 05 TR-Payment REDEFINES TR-Body. 10 PAY-Account PIC 9(8). 10 PAY-Amount PIC 9(7)V99. 10 PAY-Method PIC X(3). 10 FILLER PIC X(4). 05 TR-Refund REDEFINES TR-Body. 10 REF-Original-Ref PIC X(12). 10 REF-Amount PIC 9(7)V99. 10 REF-Reason PIC X(3). *> Text that may or may not hold a number: test the X view, then *> compute with the 9 view. 01 Input-Text PIC X(5) VALUE "00042". 01 Input-Number REDEFINES Input-Text PIC 9(5). 77 Next-Year PIC 9(4). 77 Doubled PIC 9(5). 77 Show-Amount PIC Z(6)9.99. PROCEDURE DIVISION. Main-Paragraph. DISPLAY "Text view : " Order-Date DISPLAY "Parts view : " OD-Year "/" OD-Month "/" OD-Day ADD 1 TO OD-Year DISPLAY "After ADD : " Order-Date MOVE "20241231" TO Order-Date COMPUTE Next-Year = OD-Year + 1 DISPLAY "Year after " OD-Year " is " Next-Year *> Variant 1: a payment. MOVE "P" TO TR-Type MOVE SPACES TO TR-Body MOVE 10442371 TO PAY-Account MOVE 250.00 TO PAY-Amount MOVE "CRD" TO PAY-Method PERFORM Show-Transaction *> Variant 2: a refund, written through the other view. MOVE "R" TO TR-Type MOVE SPACES TO TR-Body MOVE "INV-2026-118" TO REF-Original-Ref MOVE 75.50 TO REF-Amount MOVE "DMG" TO REF-Reason PERFORM Show-Transaction *> Class test on the text view before arithmetic on the numeric view. IF Input-Text IS NUMERIC COMPUTE Doubled = Input-Number * 2 DISPLAY "Doubled : " Doubled END-IF MOVE "4x2" TO Input-Text IF Input-Text IS NOT NUMERIC DISPLAY "Not numeric: [" Input-Text "]" END-IF STOP RUN. Show-Transaction. DISPLAY "Raw : [" Transaction-Record "]" EVALUATE TRUE WHEN TR-Is-Payment MOVE PAY-Amount TO Show-Amount DISPLAY "Payment : account " PAY-Account " amount " Show-Amount " via " PAY-Method WHEN TR-Is-Refund MOVE REF-Amount TO Show-Amount DISPLAY "Refund : ref " REF-Original-Ref " amount " Show-Amount " reason " REF-Reason WHEN OTHER DISPLAY "Unknown type " TR-Type END-EVALUATE.