COBOL Arrays – Internal Table

Arrays groups similar types of items under one table.

COBOL ARRAYS

The internal table in COBOL is called as ARRAY. The records/items which stores in the table must have similar properties i.e. PIC clause.

  • Internal table(Array) in COBOL is different from DB2 tables.
  • Arrays/Internal table are just a linear data representation of the similar type of the data. It is not a physical table.
  • The Table is divided into ROWS and COLUMNS.

We can define a Table using OCCURS Clause.  

Let us understand Table/Arrays in detail-.
  • OCCURS clause specifies the number of occurrences of an element in the table(arrays).
  • And OCCURS clause can be used with level number 02 through level number 49.
  • This clause must be defined for Group or Elementary data items only.
  • We can have multiple OCCURS Clause depending on whether we want to create a 1 Dimensional Array or multidimensional Array.
  • To access the elements of the table, we can use INDEX or SUBSCRIPT.
  • A table can have one Dimensional or multidimensional(2 or more).
  • If we use ‘OCCURS’ clause once in a table, it is one Dimensional. If it is used twice then the table is 2 dimensional. And if it is used ‘N’ times, then the table is ‘N’ dimensional.
  • The Maximum we can have 7 Dimensional arrays in COBOL.

Example of COBOL arrays

Consider an example,

Suppose, we have to define all the days in a week in a COBOL Program. we can repeat the days 7 times like below:

01 WEEK.
   05 DAY-1 PIC X(9) VALUE ‘MONDAY ’.
   05 DAY-2 PIC X(9) VALUE ‘TUESDAY ’.
   05 DAY-3 PIC X(9) VALUE ‘WEDNESDAY’.
   05 DAY-4 PIC X(9) VALUE ‘THURSDAY ’.
   05 DAY-5 PIC X(9) VALUE ‘FRIDAY ’.
   05 DAY-6 PIC X(9) VALUE ‘SATURDAY ’.
   05 DAY-7 PIC X(9) VALUE ‘SUNDAY ’.

If we define like above, it is not an effective way to of defining the elements. The better way is to define an internal table(array) with the meaningful name of the table of your choice.

For the example shown above, we can replace the above code with an internal table. We can give a name of the table, something like ‘WEEK-TABLE’ table.

We can define the above entries as-

01 WEEK-TABLE.
    05 DAY-NAME PIC X(09) OCCURS 7 TIMES.

Note: We can also code –
    05 DAY-NAME OCCURS 7 TIMES PIC X(09)
instead of
    05 DAY-NAME PIC X(09) OCCURS 7 TIMES.

so, both are same.

Sample COBOL Program to show One (1) Dimensional Array

TutorialBrain-One dimensional Table
TutorialBrain-One Dimensional Array(Internal Table) in COBOL

Output

TutorialBrain-Output of One Dimensional Array(table) in cobol
TutorialBrain-Output of One Dimensional Array(Internal Table) in COBOL

Sample COBOL Program to show Two (2) Dimensional Array

TutorialBrain-Two Dimensional Array(internal table) in COBOL program
TutorialBrain-Two(2) Dimensional Array(Internal Table) in COBOL

Output

TutorialBrain-Output of Two Dimensional Array(Internal Table) of COBOL Program
TutorialBrain-Output of Two(2) Dimensional Array(Internal Table) in COBOL
There are 2 ways to refer the elements in the array:
  • INDEX
  • SUBSCRIPT

INDEX

It is the offset or number of displacement position from the starting of the Array.
– ‘INDEX BY’ clause is used to declare INDEX. It must not be defined in WORKING-STORAGE Section.

Syntax

01 WEEK-TABLE.
   05 DAY-NAME PIC X(09) OCCURS 7 TIMES
   [ASCENDING/DESCENDING KEY IS key-1…]     [INDEXED BY index-name].

Example: In the above example,
DAY-NAME(1) has a displacement of 0
DAY-NAME(2) has a displacement of 9
DAY-NAME(3) has a displacement of 18 and so on…….

if we increment the index by 1, the length gets increase by the length defined in the PIC clause.

How we can assign a value to INDEX?​

The SET clause is used to initialize INDEX or to set the value of the INDEX.

Examples:
a) SET index-1 to index-2
b) SET index-1 to 1
c) SET index-1 UP BY 1
d) SET index-1 DOWN BY 1

Note: ‘SET’ is also used to set the value of switch to ON/OFF. It can also be used to assign TRUE/FALSE to a conditional variable.

Sample COBOL Program to show INDEXED BY Clause in Array​

TutorialBrain-INDEXED BY for accessing elements in Internal Table(Array)
TutorialBrain-INDEXED BY for accessing elements in Internal Table(Array)

Output

TutorialBrain-Output of INDEXED BY for accessing elements in Internal Table(Array)
TutorialBrain-Output of INDEXED BY Clause in Array(Internal Table) in COBOL

SUBSCRIPT​

A SUBSCRIPT is an integer data item which describes the number of occurrences of an array element in the table.

Subscript is not the actual memory location rather refers to a position of the data item. It refers the memory location a particular data item resides.
Subscript should have an entry in WORKING-STORAGE section with ‘PIC S9(04) COMP’.
To initialize the subscript, we can use a MOVE statement.

Subscript starts from number 1 and increment by 1 every time by using ADD statement.

Example:


– To refer to element 1 in the previous example, we have to give DAY-NAME(1)
– To refer to element 2 in the previous example, we have to give DAY-NAME(2) and so on.

Sample COBOL Program to show usage of Subscript in Array (Internal Table) in a COBOL Program

TutorialBrain-Subscript for accessing elements in Internal Table(Array)
TutorialBrain-Subscript for accessing elements in Internal Table(Array)

Difference between INDEX and SUBSCRIPT​

TutorialBrain-Difference between INDEX and SUBSCRIPT in Internal Table(Array)
TutorialBrain-Difference between INDEX and SUBSCRIPT in Internal Table(Array)

Tutorials for all brains!