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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Soft Bounds on a Table 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
Is there a way to do some code involving some soft bounds on a table, and not a hard-coded value?

Say:

Code:
04  TABLE-1 OCCURS 15 TIMES INDEXED BY T1-INDEX.

PERFORM VARYING T1-INDEX FROM 1 BY 1 
UNTIL T1-INDEX > FUNCTION TMAX(TABLE-1).

Where TMAX would return 15 in this case, but would return whatever the occurs is stated for.
 
Some COBOL compilers support 78-level constants that can be very useful for this sort of thing.

Some COBOL compilers support the LENGTH function. You can use that to determine the size of the table and the size of one entry and, hence, the number of occurrences in the table.

Regards.

Glenn
 
The problem with the LENGTH function is that it will only provide the length in bytes of one element of the table, and not the whole table.
 
I frequently use code like the following in Micro Focus COBOL.
Code:
01  CONSTANT-TABLE.
    05            PIC X(18) VALUE "ENTRY ONE".
    05            PIC X(18) VALUE "ENTRY TWO".
    . . .
    05            PIC X(18) VALUE "ENTRY LAST".
01  REDEFINES CONSTANT-TABLE.
78  CT-OCCURS     VALUE LENGTH OF CONSTANT-TABLE / 18.
    05  CT-ENTRY  PIC X(18) OCCURS CT-OCCURS TIMES.
 
I don't worry about hard coding the table capacity.
Why is it a problem?
I keep track of how full the table is, by keeping what I call,
the High Water Mark.
I try to avoid the term MAX, because I think it is ambiguous.
Is it the Capacity, or the High Water Mark?
Everyone does what works for them ... and this works for me.

Code:
000036*                                                                 
000037******************************************************************
000038**** Table    : 1                                             ****
000039**** Function : Tek-Tips Demo                                 ****
000040******************************************************************
000041****                                                              
000042**** CAP Capacity                                                 
000043**** HWM High Water Mark                                          
000044**** SUB Subscript                                                
000045**** ELT Element                                                  
000046****                                                              
000047 01  WS1-CAP                         PIC 9(03) VALUE 70.          
000048 01  WS1-HWM                         PIC 9(03).                   
000049 01  WS1-SUB                         PIC 9(03).                   
000050 01  WW-TABLE-1.                                                  
000051     03  WT1-ELT OCCURS 70 TIMES.                                 
000052         05  WT1-FIELD               PIC X(30).                   
000053*                                                                 
000054******************************************************************
000055**** Table    : 2                                             ****
000056**** Function : Tek-Tips Demo                                 ****
000057******************************************************************
000058****                                                              
000059**** CAP Capacity                                                 
000060**** HWM High Water Mark                                          
000061**** SUB Subscript                                                
000062**** ELT Element                                                  
000063****                                                              
000064 01  WS2-CAP                         PIC 9(03) VALUE 15.          
000065 01  WS2-HWM                         PIC 9(03).                   
000066 01  WS2-SUB                         PIC 9(03).                   
000067 01  WW-TABLE-2.                                                  
000068     03  WT2-ELT OCCURS 15 TIMES.                                 
000069         05  WT2-FIELD               PIC X(30).                   
000070*                                                                 




000242*                                                                 
000243*=================================================================
000244*                                                                 
000245 2000-PROCESS SECTION.                                            
000246*                                                                 
000247 CONTROL-PARAGRAPH.                                               
000248*                                                                 
000249     PERFORM VARYING WS1-SUB FROM 1 BY 1                          
000250         UNTIL WS1-SUB > WS1-HWM                                  
000251*                                                                 
000252             MOVE WS1-SUB TO WS2-SUB                              
000253             IF WS2-SUB > WS2-CAP                                 
000254                 DISPLAY 'Table 2 Overflow'                       
000255                 GO TO X-TERMINATE                                
000256             END-IF                                               
000257*                                                                 
000258             MOVE WT1-FIELD(WS1-SUB) TO WT2-FIELD(WS2-SUB)        
000259             MOVE WS2-SUB TO WS2-HWM                              
000260*                                                                 
000261     END-PERFORM.  
000262*                                                                 
000263 EXIT-PARAGRAPH.                                                  
000264     EXIT.
 
Most people would code a constant with the table capacity in it - that would actually be my answer.

Where this came from: This is a post I tried to answer for someone somewhere else, and I got extremely curious and couldn't find a way to dynamically enforce table bounds.

The rules the poster was under: 1) This table is in a copybook entry, which can not be changed under any circumstances (he does not have the approval to do so). 2) It must be dynamic - basically to eliminate having to change the code or forgetting to change the code if the table bounds were increased. 3) There is no group level available for the table.

The original poster came up with a rather kludgy way to do it, but I'm still wondering myself if there's a much more elegant solution.
 
I'm just wondering if you are reading more into the problem than there is.

Define 'code'. The source code? The executable code?
If the program code was not changed, but simply recomplied so that the new copybook was included, and subsequently a new executable produced, why wouldn't that qualify as not changing the 'code'?
If recompiling WAS considered as a code change, then what would be the relevance of the copybook modification, which I presume is what you mean by 'if the table bounds were increased'.
 
Define 'code'. The source code? The executable code?

The source code.

If the program code was not changed, but simply recomplied so that the new copybook was included, and subsequently a new executable produced, why wouldn't that qualify as not changing the 'code'?

See rule #1. There can be no changes in the copybook. No addition of size constants or what-not.

If recompiling WAS considered as a code change, then what would be the relevance of the copybook modification, which I presume is what you mean by 'if the table bounds were increased'.

I would assume the one who originally asked this question is trying to design to have to get out of having to have the program changed, the constant or whatever used within the program to handle the table not being changed as well.
If I have a constant of 12 in a program, it's not going to matter one bit with the max occurs at 15. But it will cause the program to be faulty.

From looking at the other thread, the OP came up with the POINTER solution (which would be my approach to it sans a function), but says POINTER types are invalid with the compiler he is working with when they are in WS.
 
And what about DEPENDING ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In terms of TERMINATE's table definition, I use the following all the time:

Code:
WS2-MAX = LENGTH OF WW-TABLE-2
              / LENGTH OF WT2-ELT (1)

But now that I know about RM/Cobol's COUNT OF special register I'll probably start using that instead.

Thanks, Tom!

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top