Array and Table

You might also like

You are on page 1of 4

Array/Table Processing

Arrays are referred to as tables in COBOL. An array is a linear data structure, which is a collection of
individual data items of the same data type.

Description for the one-dimensional and Multi-dimensional table is given below:

One-Dimensional Table

In a one-dimensional table, the ' Occurs ' clause should be specified only once in the declaration.

Syntax:

01 WS-TABLE.

05 WS-A PIC A(10) OCCURS 10 TIMES.

Here, WS-TABLE is the group item that contains the table, and WS-A names the table elements that
occur ten times.

Note: Array / Table name should be declared at level 01

Two-Dimensional Table

A two-dimensional table is generated with the variable length of both data elements.

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).

Here, the first WS-A array may occur 1 to 10 times, and the inner WS-C array may occur 1 to 5 times.
Means, there will be 5 WS-C entries for each WS-A entry.

Subscript

By using the subscript, we can retrieve the table's individual elements. The values of subscript will vary
from 1 to the number of times that the table/array occurs. Any +ve number can be a subscript value. In
the data division, there is no need for any subscript declaration. It is formed automatically with the
Occurs clause.

Index

We may also use the index to access the table elements. An index is an element that moves from the
beginning of the table. We need to describe the INDEXED BY clause with the Occurs clause to declare the
index.

Use the SET statement and the PERFORM VARYING option to change the index value.

Syntax:

01 WS-TABLE.

05 WS-A PIC A(10) OCCURS 10 TIMES INDEXED BY I.

Set Statement

The set statement changes the index value. It is used to initialize, increment, or decrement the index
value. This statement can be used with search and search all to locate elements in the table.

SET I J TO positive-number

SET I TO J
SET I TO 5

SET I J UP BY 1

SET J DOWN BY 5

Search

It is a linear method of searching. This is used for locating table elements. We can perform the search on
a sorted or unsorted table. Search is used only for tables declared by index phrase. This begins with the
index's initial value. If the searched item is not available, the index will be automatically incremented by
one and will continue until the end of the table.

Search All

Search All is a binary search method. This is used to find elements inside the table. The table must be in
sorted order in Search All. The index does not need to be initialized.

As we know, in the binary search method, the table is divided into two half sections, and it determines in
which half the searched element is present. This process repeats until the element is found or the end is
reached.

Comparison between SEARCH and SEARCH ALL

SEARCH (Serial/Linear Search) SEARCH ALL (Binary Search)

1)It does sequential search 1) It does binary search

2)Array must have INDEX 2) Array must have INDEX

3)Not mandatory for array to be in sorted order

3) Array should be in sorted order of searching parameter. It can be sorted using


ASCENDING/DESCENDING option of OCCURS clause

4)Multiple WHEN conditions can be specified 4)Only one WHEN condition can be coded

5)Condition can’t be compound condition (i.e. you cannot concatenate multiple conditions using
AND/OR) Only ‘EQUAL TO’ comparison is possible. 5)Compound condition is allowed

6)Index should be set to 1 before SEARCH statement 6)Index need not be set to 1

Preferred when:
7)Array size is small 7) Array size is large

8)Array contains duplicates 8)Array contains no duplicates

You might also like