COBOL Index & Subscript

Index & Subscript – performance considerations

Both are used to reference an array or table element in COBOL programs. By knowing the basic differences between the two, one can code the programs efficiently in performance point of view.

1. Index –> Represents the displacement of a table entry from its begining which is being set to ZERO initially to reference the array from its begining
Subscript –> Occurrence of table entry which is being given a value of 1 to reference the array from its begining.

2. To use Subscripts, it needs to be declared in working storage where as for Indexes there is no need of decalration.

3. Indexing is Binary search and Subscript is searial search…So for the larger number of records it is better to use indexing for faster search.

4. With subscript we can do both search and search all functions.where as with Indexing only search can be performed.

5. In Index concept, data will be pointed internally in binary format that is why it will not take time to point it out. where as it n’t the same with subscript, internally it has to convert into binary form and then start pointing to the record thus results are time-consuming.
when ever an array element is referred, to retrieve the value, displacement position will be calculated internally and this takes some time as it includes multiplication and in case the array size is large, there will be much time difference. So its better to go for index in case of large arrays.

Index Usage Example:

Before Search INdex needs to be set to 0 to get the table searched from the begining.


Index Value Calculation:

COBOL compiler calculates it as below
==> (Occurrence number – 1)*(Array Length).
So in the above example,  for the 3rd occurrence of array ‘WW-CUST-PHON’, Index contains a value of 10 { (3 – 1) * 5 ===> 10}.


Subscript Usage Example:

01 ws-subscrpt pic 9(04) value zeroes.

While initializing there is no need of specifying subscript anywhere. But to reference the Array elements subscript needs to be used. It is nothing but a counter to indicate different array occurances. In subscript case it can also be referenced just by specifying the number directly.

Leave a Reply

Your email address will not be published. Required fields are marked *