COBOL Index & Subscript

COBOL Index and Subscript – Performance considerations

Index and Subscript in COBOL are the very important concepts. 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 beginning which is being set to ZERO initially to reference the array from its beginning
Subscript –> Occurrence of table entry which is being given a value of 1 to reference the array from its beginning.

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

3. Indexing is Binary search and Subscript is serial 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 all 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:

01 WW-CUST-CALL-PROFILES. 
   02 WW-CUST-PHON OCCURS 5 TIMES INDEXED BY PHNE-INDX. 
      04 WW-PHONE-TYPE PIC X(01) VALUE SPACES. 
      04 WW-PHONE-CODE PIC X(05) VALUE SPACE. 
      04 WW-PHONE-NUM PIC X(10) VALUE SPACES.

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

SET PHNE-INDX TO 0

SEARCH WW-CUST-PHON VARYING PHNE-INDX 
       AT END 
          SET WF-PHON-NOT-FOUND TO TRUE
       WHEN WW-PHONE-NUM(PHONE-INDX) = 
                                         WW-INPUT-PHONE 
          CONTINUE 
END-SEARCH

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 32 { (3 – 1) * 16 ===> 32}.

Subscript Usage Example:

01 WT-CUST-CALL-PROFILES. 
   02 WT-CUST-PHON OCCURS 5 TIMES. 
      04 WT-PHONE-TYPE PIC X(01).
      04 WT-PHONE-CODE PIC X(05).
      04 WT-PHONE-NUM PIC X(10).

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 occurrences. In subscript case it can also be referenced just by specifying the number directly.
Ex: WK-PHNE-NUM(1)

3 Comments

Leave a Reply to admin Cancel reply

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

Close Bitnami banner