Page 1 of 1

What is the difference b/w Index and subscript?

Posted: Thu Oct 31, 2013 9:40 pm
by Bala Kumar
Hi,

What is the main difference between Index and subscript? I read some differences about them online but they are confusing. Can someone please brief them in a layman language.

Re: What is the difference b/w Index and subscript?

Posted: Thu Oct 31, 2013 10:12 pm
by Robert Sample
An index is a pointer that contains the address of the array element; a subscript is the relative occurrence (from 1 to whatever) of the array element. When you write

Code: Select all

SET INDEX-VAR UP BY 1
COBOL determines how long each array element is and increments the address by that length.

Sample code (untested):

Code: Select all

77  C     PIC S9(04) COMP VALUE 1.
.
.
.
05  A     OCCURS 10
          INDEXED BY B
          PIC X(16).
Suppose when the program executes, A winds up at address X'00200000', index B is set to the first entry, and C has its initial value of 1. In this case, index B's internal valoue (which you cannot see unless you get a storage dump) will be X'00200000' and C will be X'0001' respectively. You can reference the first array element of A by using A(B) or A(C). If you then code

Code: Select all

SET B UP BY 2
ADD 2 TO C
then B will have the internal value X'00200020' and C will have the value X'0003' yet both can be used to access the third element of array A.

Re: What is the difference b/w Index and subscript?

Posted: Sat Nov 02, 2013 5:42 am
by William Collins
A small point. An index does not contain an address. It just contains the displacement of an element from the first item of the OCCURS. When looking at the first entry in a table, the index does really contain zero (although the programmer never needs to know that - until looking at a dump).

Bala Kumar, this is a wide area. It would be much easier to answer if you were specific about what you understand and don't about the two things.

Re: What is the difference b/w Index and subscript?

Posted: Mon Dec 02, 2013 4:17 pm
by Bala Kumar
Thanks Robert and William for the great explanations you put it. really appreciate your time.

As Robert said, "An index is a pointer that contains the address of the array element" and William said, "An index does not contain an address. It just contains the displacement of an element from the first item of the OCCURS" . So if I need to calculate the value of an idex with the example from Robert - how would that be calculated?

Re: What is the difference b/w Index and subscript?

Posted: Tue Dec 10, 2013 9:15 pm
by Robert Sample
The displacement depends upon the length of each table element, so you must first know how long each table element is. The first table element index will always be at displacement zero. If, as in my example, the table element size is 10 bytes, the second table element index would be 10, the third would be 20, and so forth. If the table contains several variables with an aggregate size of 197 bytes, the second table element index would be 197, the third would be 394, and so forth. Note that indexes are 4-byte binary values so you would NOT see the value '197' anywhere in a dump -- the hex value would be X'000000C5' and this is what would be in memory.

YOU normally would not calculate any index -- you let the compiler calculate the index. The only time you would normally see a raw index value would be if your program has an ABEND that causes a dump, in which case you could see the displacement value in the memory allocated to the index. However, finding that location requires knowledge of how COBOL uses the Task Global Table (TGT) with index cells (variables).

Re: What is the difference b/w Index and subscript?

Posted: Thu Dec 12, 2013 12:01 pm
by William Collins
Other than solving an abend, as Robert has said, you, yourself, do not need to do any calculations with an index.

If you SET an index to 1, you can reference the first element of an OCCURS. If you SET it to 2, you can reference the second element. You don't have to do any calculation, the compiler does it all.

Again, if you can explain exactly why you think you need to do any calculation, and exactly why you want to know the difference between a subscript an an index in your case, then your question can get a better answer.