Page 1 of 1

Subscript VS Index

PostPosted: Fri Sep 03, 2010 5:36 pm
by debo
Hi,

This may be a repeated Question..But still i am not clear .So can you explain clearly...? The Main diff b/w INDEX & Subscript ?

Say,
01 Month.
02 M Occurs 12 times pic x(3). < - Values are JAN,FEB,MAR,APR,JUN,JUL,AUG,SEP,OCT,NOV.DEC.

77 sub pic x(3).

So if i want to Display only Jun monthe How?

Move 3 to SUB
Display M(SUB) Will do that ?


If i use index,then to Display JUN month,the IDX should be 18 ?

Please explain how the Displacement & Occurance comes into picture? Also Index can have value of ZERO? But Subscript should not have ZERO ?>

Re: Subscript VS Index

PostPosted: Sat Sep 04, 2010 1:15 am
by dick scherrer
Hello,

With a bit of test code you should run a few experiments.

If you do not understand what happens, post the code, what happened, and your doubt.

Someone will be able to clarify.

Re: Subscript VS Index

PostPosted: Sat Sep 04, 2010 3:39 am
by Robert Sample
01  WS-MONTH-TABLE.
    05  MONTH-SUB          PIC 9(02).
    05  WS-MONTH-CONSTANTS PIC X(36) VALUE 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.
    05  WS-MONTHS          REDEFINES WS-MONTH-CONSTANTS
                           OCCURS 12
                           PIC X(03)
                           INDEXED BY MONTH-INDEX.

MOVE 6 TO MONTH-SUB.
SET MONTH-INDEX TO 6.
DISPLAY 'SUB: ' WS-MONTHS (MONTH-SUB).
DISPLAY 'IDX: ' WS-MONTHS (MONTH-INDEX).
If you run this code, it should clear up a lot of confusion on your part. The examples you gave in your post would not display JUN for either subscript or index.

Displacement and occurrence only come into the picture if you are dealing with dumps -- in which case you have to use the table element length to determine the actual occurrence number from the index value.

Actually, both indexes and subscripts can have values that are positive, zero, or negative and do not have to be within the number of table occurrences. However, using zero or negative values or exceeding the number of table occurrences vastly increases the likelihood of incorrect results, and an abend would be quite possible.

Re: Subscript VS Index

PostPosted: Sun Sep 05, 2010 1:23 pm
by debo
Hi,

Thanks..But my doubt is: in both SUB & Index case,we set value 6...so what is the diff b/w subscript(no of occurances) & Index (( Displacement )?????

Re: Subscript VS Index

PostPosted: Sun Sep 05, 2010 6:19 pm
by Robert Sample
An index allows you to use binary search on a table (SEARCH ALL) whereas a subscript does not. While indexes are slightly more efficient than subscripts, you would have to be accessing literally millions (possibly hundreds of millions) of table entries for there to be any noticeable difference on current machines.

My advice: accept that they both exist, pick one and use it (unless you're doing a binary search and must use an index), and forget about the question. You've already spent more time and energy worrying about the topic than your programs could possibly save in the next 20 years. To echo Perl enthusiasts, there is more than one way to do it in COBOL most of the time.