Menu
CoddyTech

Reference Modification

Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 10 of 19.

Reference modification reads part of a field: after the name, the start position and the length in brackets.

01 STAMP PIC X(8) VALUE "20261123".
    DISPLAY STAMP(1:4). *> 2026
    DISPLAY STAMP(5:2). *> 11

Positions count from 1. Leaving out the length takes the rest of the field: STAMP(5:) is 1123.

Start and length can be fields, and a part can receive a MOVE:

MOVE "XX" TO STAMP(3:2). *> 20XX1123
DISPLAY STAMP(P:L).

A part that runs past the end of the field is an error. With literals, like STAMP(5:6), the compiler rejects it; with fields, the program stops when it runs:

MOVE 5 TO P.
DISPLAY STAMP(P:6).
error: length of 'STAMP' out of bounds: 6,
starting at: 5, maximum: 8

REDEFINES names the parts once for the whole program; reference modification picks a part where it is used.

challenge icon

Challenge

Easy

The program reads a 16-digit card number into CARD. Display Card ending and its last 4 digits: Card ending 1111.

Example input, one line per ACCEPT: 4111111111111111.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CARDEND.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 CARD PIC X(16).
       PROCEDURE DIVISION.
           ACCEPT CARD.
           *> Display the last 4 digits here
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tables and Text