COBOL Table

Introduction

  • Tables in COBOL are known as arrays.
  • An array is a collection of similar data items having a same type, length and declaration.
  • Data items of an array are sorted internally.

Declaration of table or array

  • Tables are declared in Data Division.
  • To define tables Occurs clause is used. This clause shows the repetition of data items.
  • Data items are declared only with the level number 02 to 49.
  • Table name declared with 01 level number and it does not have any OCCURS clause associated with it.

One dimensional table

Occurs clause is used only once in declaration of one dimensional table.

Syntax:
01 WS-TABLE
     05 WS-A PIC A(10) OCCURS 10 TIMES.

Where, WS-TABLE is the group item which contain a table. WS-A is the name element of table which occurs 10 times.

Example : Program to demonstrate One-Dimensional table

IDENTIFICATION DIVISION.
PROGRAM-ID. OA.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-TABLE.
      05 WS-A PIC A(13) VALUE 'TUTORIALRIDE' OCCURS 4 TIMES.     

PROCEDURE DIVISION.
   DISPLAY "ONE-Dimensional Table/Array : "WS-TABLE.
STOP RUN.


Output:
ONE- Dimensional Table/Array : TUTORIALRIDE TUTORIALRIDE TUTORIALRIDE TUTORIALRIDE

Two dimensional table

A two dimensional table is generated with both data elements with different length.

Syntax:
01 WS-TABLE
     05 WS-A OCCURS 10 TIMES.
           10 WS-B PIC A(10).
           10 WS-C OCCURS 5 TIMES.
                 15 WS-D PIC X(6).

Example : Program to demonstrate Two-Dimensional table

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-TABLE.
      05 WS-A OCCURS 2 TIMES.
         10 WS-B PIC A(10) VALUE ' TUTORIAL'.
         10 WS-C OCCURS 3 TIMES.
            15 WS-D PIC X(6) VALUE ' RIDE'.

PROCEDURE DIVISION.
   DISPLAY "TWO-D TABLE : "WS-TABLE.

STOP RUN.


Output:
TWO-D TABLE : TUTORIAL  RIDE  RIDE  RIDE  TUTORIAL  RIDE  RIDE RIDE