OCCURS
Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 1 of 19.
A table is many fields of the same shape under one name. OCCURS says how many:
01 SCORES.
05 SCORE PIC 9(3) OCCURS 5 TIMES.SCORES is a group of five 3-digit fields, 15 characters in all. IBM's COBOL does not allow OCCURS on a 01 level, so a table sits inside a group.
Each copy is an element. SCORE(1) is the first and SCORE(5) the last:
MOVE 42 TO SCORE(2).
DISPLAY SCORE(2). *> 042VALUE on an OCCURS item gives every element the same start. Without it the elements hold binary zeros, and adding to one stops the program.
05 SCORE PIC 9(3) OCCURS 5 TIMES
VALUE ZERO.The group is still one field. Moving text to it fills the elements in order:
MOVE "MonTueWed" TO DAYS.
DISPLAY DAY-NAME(2). *> TueChallenge
EasyThe group WEEK must hold 7 day names of 3 characters. Declare DAY-NAME under it with OCCURS, so the program prints Wed.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. WEEKDAY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WEEK.
*> Declare DAY-NAME here
PROCEDURE DIVISION.
MOVE "MonTueWedThuFriSatSun" TO WEEK.
DISPLAY DAY-NAME(3).
STOP RUN.