tags:

views:

192

answers:

2
+1  Q: 

COBOL Confusion

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!

+1  A: 

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.

Charlie Martin
Thank you very much for the reference material, Charlie. I shall review it, and I am certain that it will help me in the futureThanks!
Enyalius
+3  A: 

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 (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 character for TABLE in working storage. If TABLE-SIZE is set to 2 when the record is written to a file, only 8 characters will be written (over and above the characters written for TABLE-SIZE, of course).

Similarly, when the record is read back in, both TABLE-SIZE and the relevant bits of TABLE will be populated from the file. I don't believe that the unused TABLE entries are initialized 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.

paxdiablo
This was precisely what I was trying to understand. Thank you for your time and knowledge.
Enyalius