Menu
CoddyTech

INDEXED BY

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

A table can carry its own index, named in the OCCURS clause. An index has no PIC:

05 GUEST PIC X(5) OCCURS 4 TIMES
    INDEXED BY GX.

A subscript is a number that the program turns into a place in storage each time. On a mainframe, an index already holds that place, an offset into its own table, so it is the faster way to step through a table.

SET changes an index: to a position, or up and down by a step.

SET GX TO 2.
SET GX UP BY 1.
DISPLAY GUEST(GX). *> third

PERFORM VARYING steps an index like a counter. To print the position, SET a numeric field from the index:

PERFORM VARYING GX FROM 1 BY 1 UNTIL GX > 4
    SET PLACE TO GX
    DISPLAY PLACE
END-PERFORM.
challenge icon

Challenge

Easy

The program reads a position from 1 to 3 into N. Point GX at it, move GX up by one and display the guest there: 1 prints Next: Bob.

Example input, one line per ACCEPT: 1.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. NEXTONE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 GUESTS.
           05 GUEST PIC X(5) OCCURS 4 TIMES
               INDEXED BY GX.
       01 N PIC 9.
       PROCEDURE DIVISION.
           MOVE "Ann  Bob  Cyd  Dee  " TO GUESTS.
           ACCEPT N.
           *> Use GX to display the next guest
           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