IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

COBOL Index issue


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Nagendran

New User


Joined: 24 Jan 2007
Posts: 89
Location: USA

PostPosted: Thu Mar 12, 2009 1:19 am
Reply with quote

I have the below array.

Code:
01  WS-TABLE-A.
      05  WS-COL-A  OCCURS 9000 times indexed by WS-A
      05  WS-COL-B  OCCURS 9000 times.


Is it possible for me to access the array WS-COL-B using the index WS-A.

Can anybody explain?

Thanks,
Nagendran.R
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Mar 12, 2009 1:33 am
Reply with quote

Hello,

Quote:
Is it possible for me to access the array WS-COL-B using the index WS-A.
No.
Quote:
Can anybody explain?
The second array has no index, so it would be subscripted rather than indexed.

Neither array has any elementary items. . . If you want to continue the topic, suggest you post something more realistic. . .
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Thu Mar 12, 2009 1:35 am
Reply with quote

Is this an Interview question?

If it is, it certainly doesn't represent realistic coding (I hope), unless it's an example of extremely poor coding techniques.

If this were to be presented during a code-review, suffice to say it would be immediately rejected. In some shops, your A$$ would be fired on the spot!

But to answer your question, NO.

Bill
Back to top
View user's profile Send private message
Nagendran

New User


Joined: 24 Jan 2007
Posts: 89
Location: USA

PostPosted: Thu Mar 12, 2009 1:41 am
Reply with quote

Hi

Sorry for the confusion..

Below is the code

01 WS-TABLE-A.
05 WS-COL-A PIC X(09) OCCURS 9000 times indexed by WS-A
05 WS-COL-B PIC X(02) OCCURS 9000 times.

IS it possible for me to the access thh array WS-COL-B using the index WS-A.

If not can any body tell me why????
As per my understanding index will always have displacement.......

Thanks,
Nagendran
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Thu Mar 12, 2009 1:42 am
Reply with quote

Nagendran wrote:
Code:
01  WS-TABLE-A.
      05  WS-COL-A  OCCURS 9000 times indexed by WS-A
      05  WS-COL-B  OCCURS 9000 times.

Is it possible for me to access the array WS-COL-B using the index WS-A.
I think you can......
Quote:
Can anybody explain?
The INDEXED BY phrase specifies the indexes that can be used with a table. A table without an INDEXED BY phrase can be referred to through indexing by using an index-name associated with another table.
And
An index-name can be used to reference any table. However, the element length of the table being referenced and of the table that the index-name is associated with should match. Otherwise, the reference will not be to the same table element in each table, and you might get runtime errors. .
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Thu Mar 12, 2009 2:08 am
Reply with quote

Even though you possibly CAN, I would NOT as it can confuse others.

Why in the Wide World of Sports would you do this anyway?

It certainly doesn't represent a technique (IMHO) which would pass code review.

Why add this confusion? It makes no sense whatsoever and you may find colleagues asking you the same. You could simply assign a separate index to the second array and be done with it _O R_ eliminate the first index and resort to subscripts.

You need to think of the next poor slob who needs to maintain your "idea", especially in an ABEND situation when he/she must resolve this index displacement and determine which array it was pointing to when the program went belly-up. icon_surprised.gif

Stick with the KISS method -

"A method to fend off creeping featurism and control complexity of development. "Keep It Simple Stupid".

Bill
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Mar 12, 2009 2:13 am
Reply with quote

Hello,

Quote:
Even though you possibly CAN, I would NOT as it can confuse others.
And with the example provided, it would run "wrong", possibly not get caught in testing, and would be a nightmare to figure out when the eventual problem/abend occurred.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Thu Mar 12, 2009 2:22 am
Reply with quote

Nagendran wrote:
As per my understanding index will always have displacement.......
The same links pointed to by CG mention that indexes contain the occurrence number, not the actual displacement.

BTW, I still find it interesting that I can still learn new things and used properly, I see no problem with it.

Dick wrote:
And with the example provided, it would run "wrong"
Sorry I missed that, but huh?
Back to top
View user's profile Send private message
Nagendran

New User


Joined: 24 Jan 2007
Posts: 89
Location: USA

PostPosted: Thu Mar 12, 2009 2:26 am
Reply with quote

thank you all for ur comments
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Thu Mar 12, 2009 2:37 am
Reply with quote

William Thompson wrote:
The same links pointed to by CG mention that indexes contain the occurrence number, not the actual displacement.

I interpret that to mean that the index corresponds to an occurrence number but it actually consists of the displacement (4-byte binary). If the index is displayed or used in an arithmetic comparison, it will display as and act as it it's an occurrence number though.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Mar 12, 2009 3:10 am
Reply with quote

Hello,

Quote:
Sorry I missed that, but huh?
The example arrays have elements of different length.

Quote:
However, the element length of the table being referenced and of the table that the index-name is associated with should match. Otherwise, the reference will not be to the same table element in each table, and you might get runtime errors. .
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Thu Mar 12, 2009 3:13 am
Reply with quote

Terry Heinze wrote:
I interpret that to mean that the index corresponds to an occurrence number but it actually consists of the displacement (4-byte binary). If the index is displayed or used in an arithmetic comparison, it will display as and act as it it's an occurrence number though.
I think that he was refering to this line:
At run time, the contents of the index corresponds to an occurrence number for that specific dimension of the table with which the index is associated.
Back to top
View user's profile Send private message
Douglas Wilder

Active User


Joined: 28 Nov 2006
Posts: 305
Location: Deerfield IL

PostPosted: Thu Mar 12, 2009 3:13 am
Reply with quote

In order to tell how this would work we need the elementary item definitions.

In the following the same index may be used for both and it should work ok:
Code:
01  WS-TABLE-A.
      05  WS-COL-A  OCCURS 9000 times indexed by WS-A.
          10 PIC X.
      05  WS-COL-B  OCCURS 9000 times.
          10 PIC X.


In the following 2 examples if the same index is used for both it would not work as someone might expect them to:
Code:
01  WS-TABLE-A.
      05  WS-COL-A  OCCURS 9000 times indexed by WS-A.
          10 PIC X.
      05  WS-COL-B  OCCURS 9000 times.
          10 PIC X(2).

Code:
01  WS-TABLE-A.
      05  WS-COL-A  OCCURS 9000 times indexed by WS-A.
      05  WS-COL-B  OCCURS 9000 times.
          10 PIC X.

This last one is the closest I could assume from the original question.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Mar 12, 2009 3:39 am
Reply with quote

The COBOL Language Reference manual (link at the top of the page) is quite clear in stating that you can reference WS-COL-B using WS-A index. However, if the table elements are not the same size, as in Douglas Wilder's second example, the COBOL manual states you will not be referencing the same element in both tables and can get runtime errors as a result.

So you can do it, but it can be a source of production abends and therefore the recommendation is very strongly not to do it.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top