Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

internal table using an index

Status
Not open for further replies.

nxm150

Programmer
May 22, 2002
78
US
Help. I am drawing a blank. I have built an internal table using an index. I now want to display the contents of the table. How do I do this?
 
Hi nxm150,

If you have used an index to build the table, you obviously know how to reference using an index, so what you don't know can only be how to display. Have you tried using DISPLAY?

If that's not what you want then perhaps you could provide a bit more detail.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Table is -

01 WS-INTERNAL-CONVERSION-TBL.
05 WS-TBL-VALUE
OCCURS 20 TIMES
INDEXED BY WS-TABLE-SIZE.
10 WS-TBL-DB2-ALIAS PIC X(18).


After building the table, to do a display of the table, do I do..

PERFORM 1000-DISPLAY-TABLE
VARYING WS-SUB FROM 1 BY 1
UNTIL WS-SUB > WS-TABLE-SIZE.

1000-DISPLAY-TABLE.
DISPLAY WS-TBL-DB2-ALIAS (WS-SUB).

My question is when doing the display, can I use a subscript to reference the field or do I have to use the index.
 
You can use a subscript, but if you use the index in the PERFORM and DISPLAY statements, the compiled code will be more efficient. That's what an index is for.

From your sample code, it appears that you are confused as to the function of the index. Could you show the code you use to build the table?
 
Pretty simple..I am processing a DB2 cursor and am doing...

Set WS-TABLE-SIZE to 1.
Perform Build-internal-table
until SQLCODE = +100.

Build-internal-table.
Move DB2-RESULTS TO WS-TBL-DB2-ALIAS (WS-TABLE-SIZE)
Set WS-TABLE-SIZE up by 1.
 
Hi nxm150,

The basic loop for a Display (or any other process on indexed table entries) is exactly as you have - I'm assuming you also have other logic for processing your cursor and table full error checking, etc.

Given its name, you seem to want to use your index (once you have loaded the table) as a means to tell when other processes have reached the end of the table. It's not the method I would choose but it will work and the only point I'd make is that it will actually be pointing to the first UNused entry at the end of your load process so you may want to set it down by 1 before doing anything else.

Now, if you are using the WS-TABLE-SIZE index in this way, you cannot use it for any other purpose. Subject to WS-TABLE-SIZE actually pointing to the last used entry (see above) your code with the WS-SUB subscript will work; to change it to use an index, you need to define a(nother) index on the table and then use that in your loop instead of WS-SUB ..

Code:
[blue]01  WS-INTERNAL-CONVERSION-TBL.                 
    05  WS-TBL-VALUE                            
           OCCURS 20 TIMES                      
           INDEXED BY [red]WS-NDX,[/red] WS-TABLE-SIZE.            
        10  WS-TBL-DB2-ALIAS        PIC X(18).  


:
:
:


PERFORM 1000-DISPLAY-TABLE
   VARYING [red]WS-NDX[/red] FROM 1 BY 1 
       UNTIL [red]WS-NDX[/red] > WS-TABLE-SIZE.

1000-DISPLAY-TABLE.
   DISPLAY WS-TBL-DB2-ALIAS ([red]WS-NDX[/red]).[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks for the example. That is exactly what I was looking for. I don't think I need to SET my INDEX DOWN BY 1 cause I only SET WS-TABLE-SIZE UP BY 1 when my SQLCODE = 0.
 
You still need to SET WS-TABLE-SIZE DOWN BY 1 at the end of the build process as it points to the next available entry, which means that at the end of the loading, it will point to the first unused entry as Tony said. Note that if there is no data in the external table, WS-TABLE-SIZE is set to 1, when it should be 0.

Alternatively, you could use UNTIL WS-NDX >= WS-TABLE-SIZE.

Also, I note that if SQLCODE gets a value other than Zero or +100, which probably would indicate some type of error other than END, the code will stll continue to process. You probably should include some error processing logic.
 
Yes, you are right, I still need to SET my index down by 1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top