Arrays groups similar types of items under one table.
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:
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-
05 DAY-NAME PIC X(09) OCCURS 7 TIMES.
Note: We can also code –
05 DAY-NAME OCCURS 7 TIMES PIC X(09)
05 DAY-NAME PIC X(09) OCCURS 7 TIMES.
so, both are same.
Sample COBOL Program to show One (1) Dimensional Array
Sample COBOL Program to show Two (2) Dimensional Array
There are 2 ways to refer the elements in the array:
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.
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.
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
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.
– 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.