What is the difference b/w Index and subscript?

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Bala Kumar
Registered Member
Posts: 16
Joined: Sun Oct 20, 2013 2:04 pm

What is the difference b/w Index and subscript?

Post 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.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1891
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

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

Post 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.
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

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

Post 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.
Bala Kumar
Registered Member
Posts: 16
Joined: Sun Oct 20, 2013 2:04 pm

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

Post 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?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1891
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

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

Post 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).
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

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

Post 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.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”