Defining free table index



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Defining free table index

Postby bravedreamer » Fri Jun 04, 2010 12:49 pm

Hi there!

I'm trying to get familiar with the accessing modes for tables. I would like to use theses 'free indices' (declared on level 77) but didn't manage it. How can I use this kind of indices?

I have the following strukture and I would like to access all attributes in the same loop with only one index:

 01  MY-STRUCTURE.                                             
     10 ATTRIBUTE-A       OCCURS 50   PIC X(5).                   
     10 ATTRIBUTE-B       OCCURS 50   PIC X(8).                   
     10 ATTRIBUTE-C       OCCURS 50   PIC X(26).                 
     10 ATTRIBUTE-D       OCCURS 50   PIC S9(5)V USAGE COMP-3.   
     10 ATTRIBUTE-E       OCCURS 50   PIC S9(5)V USAGE COMP-3.   
     10 ATTRIBUTE-F       OCCURS 50   PIC X(8).                   
     10 ATTRIBUTE-G       OCCURS 50   PIC S9(11)V USAGE COMP-3.   


I tried to define the index as follows:
77 INX-B    USAGE IS INDEX.


And access the elements as follows:
PERFORM VARYING INX-B FROM 1 BY 1 UNTIL INX-B > 10
   DISPLAY ATTRIBUTE-A (INX-B)                         
END-PERFORM


But it doesn't really work. I get the following errors:
"INX-B" was defined as a type that was invalid in this context.  The statement was discarded.
                                                         
"INX-B (INDEX DATA ITEM)" was compared with "10".  The comparison was discarded.
                                                         
Not enough subscripts or indices were specified for "ATTRIBUTE-A".  A subscript or index value of 1 was assumed
for each missing subscript or index.                     


How does this thing work with the indices declared on level 77??

BTW, I'm working with the Cobol Enterprise Z-OS compiler 3.4.1
bravedreamer
 
Posts: 14
Joined: Wed Nov 11, 2009 6:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Defining free table index

 

Re: Defining free table index

Postby Robert Sample » Fri Jun 04, 2010 4:26 pm

The COBOL Language Reference manual says this about USAGE INDEX variables:
5.3.17.5 INDEX phrase

A data item defined with the INDEX phrase is an index data item.

An index data item is a 4-byte elementary item that can be used to save index-name values for future reference. An index data item is not necessarily connected with any specific table. Through a SET statement, an index data item can be assigned an index-name value. Such a value corresponds to the occurrence number in a table.

Direct references to an index data item can be made only in a SEARCH statement, a SET statement, a relation condition, the USING phrase of the procedure division header, or the USING phrase of the CALL or ENTRY statement.
The PERFORM VARYING statement you used is not allowed under the COBOL syntax rules described in the manual.

I recommend you find the COBOL manuals on the IBM web site http://www.s390.ibm.com and bookmark them so you can look up things yourself in the future.
Robert Sample
Global moderator
 
Posts: 3376
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 224 times

Re: Defining free table index

Postby bravedreamer » Fri Jun 04, 2010 5:22 pm

Hmmm... now I'm a bit confused.

I found an example in the programming guide where an index is used exactly in the PERFORM VARYING statement.

Indexing
You can create an index either with a particular table (using OCCURS INDEXED BY) or
separately (using USAGE IS INDEX).
For example:
05 TABLE-ITEM PIC X(8)
OCCURS 10 INDEXED BY INX-A.

The compiler calculates the value contained in the index as the occurrence number
(subscript) minus 1, multiplied by the length of the table element. Therefore, for
the fifth occurrence of TABLE-ITEM, the binary value contained in INX-A is (5 - 1) * 8,
or 32.
You can use this index to index another table only if both table descriptions have
the same number of table elements, and the table elements are the same length.
If you use USAGE IS INDEX to create an index, you can use the index data item with
any table. For example:
77 INX-B USAGE IS INDEX.
. . .
PERFORM VARYING INX-B FROM 1 BY 1 UNTIL INX-B > 10
DISPLAY TABLE-ITEM (INX-B)
. . .
END-PERFORM.

INX-B is used to traverse table TABLE-ITEM above, but could be used to traverse
other tables also.


So, why can't I use this example? Is it maybe from another compiler version? Or do I have to use it in another context?
bravedreamer
 
Posts: 14
Joined: Wed Nov 11, 2009 6:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Defining free table index

Postby Robert Sample » Fri Jun 04, 2010 5:27 pm

Find the difference between INDEXED BY and USAGE INDEX and you'll understand why the one case is allowed and the other case is not.
Robert Sample
Global moderator
 
Posts: 3376
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 224 times

Re: Defining free table index

Postby bravedreamer » Fri Jun 04, 2010 5:53 pm

Sorry, but I don't really get it.

I mean, from the example given above, I CAN use an index INX-B in a PERFORM VARYING phrase. But do I have to assign it before to an index created through INDEXED BY (SET INX-B to INX-A)? Is INX-B only used for storing index values? Can I therefore use INX-B only in SET statements?

thanks for Your help.
bravedreamer
 
Posts: 14
Joined: Wed Nov 11, 2009 6:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Defining free table index

Postby Robert Sample » Fri Jun 04, 2010 6:21 pm

As the manual quote in my earlier post indicates, if you create an index via USAGE INDEX in COBOL, you can use that index only for:
    SEARCH statement
    SET statement
    relation condition
    PROCEDURE DIVISION USING
    CALL ... USING
If you create an index via INDEXED BY in COBOL, you can use that index for pretty much anything that a subscript would be used for. This would include everything USAGE INDEX variables can be used for, along with using it in statement involving COBOL verbs such as PERFORM, ADD, SUBTRACT, MOVE, and so forth.

Why the distinction between the different forms of index variable? Because that is the way COBOL works. You may not want it to work that way, but the way COBOL works is the way COBOL works.
Robert Sample
Global moderator
 
Posts: 3376
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 224 times

Re: Defining free table index

Postby bravedreamer » Tue Jun 08, 2010 12:27 pm

So I don't really understand why they put such an example in the section about 'accessing tables'.

Anyway, it seems like you said: its just the way COBOL works. ;)

Thanks for your explanation.
bravedreamer
 
Posts: 14
Joined: Wed Nov 11, 2009 6:44 pm
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post