Level Numbers
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 1 of 23.
Business data comes in records: an employee has an id, a name and a pay rate. A record is one item with smaller items inside it, and level numbers show which item belongs to which:
01 EMPLOYEE.
05 EMP-ID PIC 9(4).
05 EMP-NAME PIC X(12).
05 EMP-RATE PIC 99V99.01 starts the record, in Area A. An item with a higher number belongs to the nearest item above it with a lower number, so all three 05 items belong to EMPLOYEE. They are usually indented into Area B.
Any number from 02 to 49 works under 01. Programs count in steps of 5, which leaves room to add a level later. Levels can go deeper:
05 EMP-START.
10 START-YEAR PIC 9(4).
10 START-MONTH PIC 99.An item with a PIC is elementary: it holds the data. An item with no PIC, like EMP-START, is a group made of the items under it. Statements use an elementary item by its name, like any field:
MOVE 2019 TO START-YEAR.Challenge
EasyThe record BOOK has no items yet. Give it two: BOOK-TITLE, 12 characters with the value Dune, and BOOK-YEAR, 4 digits with the value 1965. The program then prints both.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. BOOKS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BOOK.
*> Declare BOOK-TITLE and BOOK-YEAR here
PROCEDURE DIVISION.
DISPLAY BOOK-TITLE.
DISPLAY BOOK-YEAR.
STOP RUN.