VALUE, REDEFINES, RENAMES & 88 Levels
Figurative constants: ZERO/ZEROS/ZEROES, SPACE/SPACES, HIGH-VALUE(S) (highest collating character, X'FF'), LOW-VALUE(S) (X'00'), QUOTE(S), ALL literal, NULL (pointers).
10.1 VALUE
Figurative constants: ZERO/ZEROS/ZEROES, SPACE/SPACES, HIGH-VALUE(S) (highest collating character, X'FF'), LOW-VALUE(S) (X'00'), QUOTE(S), ALL literal, NULL (pointers).
Constants: COBOL 2002 defines 01 CON-MAX-ROWS CONSTANT AS 100. (supported by GnuCOBOL, which also accepts the Micro Focus 78 level); in IBM code, constants are usually ordinary items with VALUE that are never modified.
01 WS-COMPANY PIC X(20) VALUE "ACME CORP".
01 WS-RATE PIC V999 VALUE .075.
01 WS-MAX-ITEMS PIC 9(4) VALUE 500.
01 WS-LINE PIC X(80) VALUE ALL "-".
01 WS-BLANKS PIC X(10) VALUE SPACES.
01 WS-ZERO-AMT PIC S9(5)V99 VALUE ZERO.
01 WS-HIGH PIC X(5) VALUE HIGH-VALUES.10.2 REDEFINES
REDEFINES gives the same storage a second description. Common uses: viewing a date as parts, overlaying record types, or initializing a table with literals.
Rules: the redefining entry must immediately follow the redefined one at the same level; it must not be larger than the original at the 01 level in older compilers (GnuCOBOL and IBM allow a larger redefinition at level 01 in WORKING-STORAGE but warn); VALUE is not allowed in a redefining item (except 88 levels).
01 WS-DATE-NUM PIC 9(8).
01 WS-DATE-PARTS REDEFINES WS-DATE-NUM.
05 WS-YEAR PIC 9(4).
05 WS-MONTH PIC 99.
05 WS-DAY PIC 99.
* Multiple record types in one file record
01 TRANS-RECORD.
05 TRANS-TYPE PIC X.
05 TRANS-DATA PIC X(79).
05 TRANS-ADD REDEFINES TRANS-DATA.
10 ADD-CUST-ID PIC 9(6).
10 ADD-CUST-NAME PIC X(30).
10 FILLER PIC X(43).
05 TRANS-PAY REDEFINES TRANS-DATA.
10 PAY-CUST-ID PIC 9(6).
10 PAY-AMOUNT PIC S9(7)V99.
10 FILLER PIC X(64).10.3 RENAMES (level 66)
66 entries come immediately after the record they rename. RENAMES is rarely used in new code — REDEFINES or a group item is usually clearer.
01 EMPLOYEE-REC.
05 EMP-ID PIC 9(5).
05 EMP-FIRST PIC X(15).
05 EMP-LAST PIC X(20).
05 EMP-DEPT PIC X(4).
66 EMP-FULL-NAME RENAMES EMP-FIRST THRU EMP-LAST.
66 EMP-KEY RENAMES EMP-ID.10.4 88-level condition names
88 levels make conditions self-documenting and keep magic values in one place — use them for every flag and code.
01 WS-MARITAL-STATUS PIC X.
88 SINGLE VALUE "S".
88 MARRIED VALUE "M".
88 DIVORCED-OR-WIDOWED VALUE "D" "W".
88 VALID-STATUS VALUE "S" "M" "D" "W".
01 WS-SCORE PIC 999.
88 GRADE-A VALUE 90 THRU 100.
88 GRADE-B VALUE 80 THRU 89.
88 FAILING VALUE 0 THRU 59.
01 WS-EOF-SWITCH PIC X VALUE "N".
88 END-OF-FILE VALUE "Y"
WHEN SET TO FALSE IS "N".IF MARRIED
DISPLAY "Married"
END-IF
IF NOT VALID-STATUS
DISPLAY "Invalid status code"
END-IF
* Moves "Y" to WS-EOF-SWITCH:
SET END-OF-FILE TO TRUE
* Moves "N" (requires the WHEN SET TO FALSE clause):
SET END-OF-FILE TO FALSE