Part 2
Data and Tables
Group items, OCCURS, REDEFINES, editing, and the data description entries that make COBOL what it is.
Part 2 is about the DATA DIVISION, the half of a COBOL program that does the most work and gets the least attention from newcomers. Its five lessons describe data rather than algorithms: how level numbers build a record, how an item repeats, how one area of storage is read two ways, how a number is dressed for a person to read, and how characters are changed inside a field.
Begin with group items and elementary items. It builds an 80-byte customer record with its byte offsets and shows why a MOVE to a group copies bytes while a MOVE to an elementary item converts and pads. Edited pictures follows on: it moves the same five values into eight numeric-edited items and tabulates every result. OCCURS and subscripts builds a table of monthly totals, a three-by-four grid and a table sized with DEPENDING ON. REDEFINES lays a second description over the same bytes, a date as text and as three numbers, then record variants switched by a type code. The newest lesson, INSPECT REPLACING and CONVERTING, cleans an order export in place, and explains why INSPECT can never make a field longer or shorter.
These lessons assume the PICTURE clause from Fundamentals, and the Files and Records part puts their record layouts into files. Planned next here: SEARCH ALL over a sorted table, and packed decimal (COMP-3) and binary (COMP) fields explained with byte dumps.
2.1
COBOL group items and elementary items: the record layout as a struct
How level numbers 01 to 49, 77 and 88 build a record; what a group move does to the bytes; a worked 80-byte customer record with its byte offsets.
7 min
2.2
COBOL edited pictures: money with Z, comma, period, CR and minus
Numeric-edited PICTURE strings format amounts for people. Eight edited items receive the same five values; a table shows every result and the rules that produce it.
6 min
2.3
COBOL OCCURS and subscripts: a table of monthly totals
OCCURS builds a table; subscripts index it from 1. A worked program sums twelve monthly totals, finds the best month, fills a 3-by-4 grid and uses DEPENDING ON.
5 min
2.4
COBOL REDEFINES example: one memory area, two views of a date
REDEFINES overlays a second description on the same bytes: a date as text and as three numbers, record variants switched by type code, and a NUMERIC test first.
5 min
2.5
COBOL INSPECT REPLACING and CONVERTING, BEFORE and AFTER
A GnuCOBOL program that cleans an order export in place with INSPECT REPLACING, CONVERTING and BEFORE/AFTER, and the size and order rules behind them.
14 min