Page 1 of 4

How to initialize the array/table in cobol ?

PostPosted: Tue Aug 30, 2011 3:44 pm
by gauthamnagpur18
Hi ,

I came across following code in production .

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 999999
MOVE SPACES TO WS-PLAN(I)
MOVE ZEROES TO WS-MEMBER(I)
END-PERFORM.

Is there any alternate way to initialise ? so we can reduce cpu consumption. :)

Regards,
Gautham

Re: How to initialize the array/table in cobol ?

PostPosted: Tue Aug 30, 2011 5:39 pm
by BillyBoyo
Is this known to be consuming lots of time?

What are the data definitions for the table?

Is the "idiotically named" "I" an Index?

How many times is this done in the program per run?

There are some alternatives, if you can answer the above.

Re: How to initialize the array/table in cobol ?

PostPosted: Wed Aug 31, 2011 7:30 am
by gauthamnagpur18
Hi Billlyboyo ,

Ya it's consuming more than 5 mins of CPu time. :o

Declaration :

01 WS-FILLER.
05 FILLER OCCURS 999999 TIMES. :roll:
10 WS-PLAN PIC X(08).
10 WS-MEMBER PIC 9(07).

Thanks,
Gautham

Re: How to initialize the array/table in cobol ?

PostPosted: Wed Aug 31, 2011 8:22 am
by dick scherrer
Hello,

You might consider the following:

01  WS-FILLER-INIT.
    05 FIRST-ITEM.
       10 FILLER    PIC X(8) VALUE SPACES.
       10 FILLER    PIC 9(7) VALUE ZEROS.
    05 THE-REST-OF-THE-ITEMS PIC X(15) OCCURS 999998 TIMES.

01 WS-FILLER REDEFINES WS-FILLER-INIT.
    05 FILLER OCCURS 999999 TIMES. 
       10 WS-PLAN PIC X(08).
       10 WS-MEMBER PIC 9(07).

PROCEDURE DIVISION.
MOVE WS-FILLER-INIT TO THE-REST-OF-THE-ITEMS.


I've not done this for a while, but it has worked for many years - hopefully, it still will ;)

The cpu usage for the initialization will practically disappear. . .

Re: How to initialize the array/table in cobol ?

PostPosted: Wed Aug 31, 2011 12:10 pm
by BillyBoyo
Just first check that you are not doing the initialisation more often than required.

Also, why PIC 9(7)? If you are not calculating with it, why not X(7). If you are calculating with it, why not COMP-3?

Dick's is the technique I used for many years. I used a slightly different data definition, but the effects are identical.

Here, I have tried to "rework" the code to "modernise" it a bit, using "reference modification".

Unfortunately, I am unable to test it. Fortunately, it is very easy to test (use your existing loop, but include a test for each element for space or zero as appropriate).

The point in using the reference modification in this way it to avoid the problem with the other technique, which is someone changes the sizes of the table or items in it, and doesn't change the initialisation. This way, it should be automatic, and you can test that as well. It won't work automatically for adding a new field to the table :-), but then it is not magic!

       
        01  W-DISPLAY-WHEN-COMPILED PIC X(8)BX(8).
        01  W-LENGTH-OF-FIRST-OCCURENCE COMP PIC S9(8).
        01  W-START-OF-REMAINING-TABLE COMP PIC S9(8).
        01 WS-FILLER.
           05 FILLER OCCURS 999999 TIMES. 
              10 WS-PLAN PIC X(08).
              10 WS-MEMBER PIC 9(07).
        PROCEDURE DIVISION.
            MOVE WHEN-COMPILED TO W-DISPLAY-WHEN-COMPILED
            DISPLAY "yourprogramnamehere " W-DISPLAY-WHEN-COMPILED

* this block of code initialises a table at "one shot". It may be put in a prargraph/section and performed as often as needed.

            MOVE SPACE TO WS-PLAN ( 1 )
            MOVE ZERO TO WS-MEMBER ( 1 )

            COMPUTE W-LENGTH-OF-FIRST-OCCURENCE = LENGTH OF WS-PLAN
                                                + LENGTH OF WS-MEMBER
            COMPUTE W-START-OF-REMAINING-TABLE
                       = W-LENGTH-OF-FIRST-OCCURENCE
                       + 1

            MOVE WS-FILLER ( 1 : W-LENGTH-OF-FIRST-OCCURENCE )
                            TO WS-FILLER ( W-START-OF-REMAINING-TABLE : )

Re: How to initialize the array/table in cobol ?

PostPosted: Wed Aug 31, 2011 12:26 pm
by BillyBoyo
Just thought, although you can't initialise automatically if someone adds an item to the table, you can discover that it has happened.

I'll leave you to do the code, from this:

Give your FILLER OCCURS a (meaningful) name.
Find the length of that new item.
Compare it to the length calculated for the first occurence by adding-up your fields.
If they are not equal, produce a diagnostic message and stop/abend.

Re: How to initialize the array/table in cobol ?

PostPosted: Wed Aug 31, 2011 2:50 pm
by BillyBoyo
One last, thing. You can also look at the INITIALIZE verb in Cobol. It used to generate a loop to do it, but maybe worth checking for these days.

With the above technique, as with any of them, you don't have to initialise to space/zero, you can do it for any starting value.

Another posibility, with the chance that things are still slow, is to "initialise as you need". Initialise only the first occrence. Then when putting something in the first, you initilise the next. I don't think you'll need to use this, as the above should perform well enough, and makes it clear when you are looking at a dump that all the data belongs to the current situation. Doing "as you need" means leaving "old" data in place. Obviously your current subscript/index should never point to the old data, but in the dump you might start to wonder.

Also, were you using a subscript in your loop? If so, how was it defined?

Re: How to initialize the array/table in cobol ?

PostPosted: Fri Sep 02, 2011 1:23 am
by gauthamnagpur18
Hi Billyboyo ,

Thanks for detailed explaination . I will try out and let you know .

I also referred http://publib.boulder.ibm.com/infocenter/wdzinfo/v7r0/index.jsp?topic=/com.ibm.ent.cbl.zos.doc/topics/tpbeg10.htm in which they explained about initialisation of table. Hope it would be useful .

Thanks ,
Gautham :ugeek:

Re: How to initialize the array/table in cobol ?

PostPosted: Tue Sep 04, 2012 12:56 am
by wicherc
Hi BillyBoyo!
I was looking for a solution to this problem and found this post.
May be an error in the sentence you wrote?

MOVE WS-FILLER ( 1 : W-LENGTH-OF-FIRST-OCCURENCE )
TO WS-FILLER ( W-START-OF-REMAINING-TABLE : )

I supose the subscript in the variable ws-filler is incomplete, isnĀ“t it?
Thanks for your response.

Re: How to initialize the array/table in cobol ?

PostPosted: Tue Sep 04, 2012 1:23 am
by BillyBoyo
Well, for a start, it is not subscripts that are being used... Have a look at the ":". Find out what it means.

You could test it yourself as well.