Question

Hey, everyone. I'm having a bit of trouble in a coding project that I'm trying to tackle in the zOS environment using COBOL. I need to read a file in and put them into an indexed table (I know there will be less than 90 records).

The thing that is throwing me is that we are bound by the parameters of the project to use a variable called "Table-Size" (set to zero at declaration).

Given all that, I need to do something like "Occurs 1 to 90 times Depending on Table-Size", but I don't understand how that will work if table-size has to (as far as I can tell) because table-size is incremented along with each entry that is added to the table. Can anyone please clear this up for me?

Thanks!

Was it helpful?

Solution

It sounds like your primary concern is: how does the compiler know how much to allocate in the array if the size changes at run-time?

The answer is that it allocates the maximum amount of space (enough for 90 entries). Note that this is for space in working storage. When the record is written to a file, only the relevant portion is written.

An example:

01  TABLE-SIZE  PIC 9
01  TABLE OCCURS 1 TO 9 TIMES DEPENDING ON TABLE-SIZE
    03 FLD1  PIC X(4)

This will allocate 36 characters (9 multiplied by 4) for TABLE in working storage. If TABLE-SIZE is set to 2 when the record is written to a file, only 8 characters of TABLE will be written (over and above the characters written for TABLE-SIZE, of course).

So, for example, if the memory occupied by TABLE was AaaaBbbbCcccDdddEeeeFfffGgggHhhhIiii, the date written to the file may be the shortened (including size): 2AaaaBbbb.

Similarly, when the record is read back in, both TABLE-SIZE and the relevant bits of TABLE will be populated from the file (setting only the size and first two elements).

I don't believe that the unused TABLE entries are initialised to anything when that occurs. It's best to assume not anyway, and populate them explicitly if you need to add another item to the table.

For efficiency, you may want to consider setting the TABLE-SIZE to USAGE IS COMP.

OTHER TIPS

We don't have quite enough information here, but the basic thing is that the variable named in the DEPENDING ON clause has to have a count of the variable number of groups. So you need something like

01   TABLE-SIZE     PIC 99
01   TABLE OCCURS 1 TO 90 TIMES
       DEPENDING ON TABLE-SIZE
    03 FIELD-1
    03 FIELD-2

and so on.

See this article or this article at Publib.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top