Page 1 of 2

Indexed Array issue

PostPosted: Wed Feb 16, 2011 6:51 pm
by pacha
I am facing an issue while trying to load data to a indexed table.

Table
-----------------------------------
01 WS-ATTR-ARRAY.
10 WS-ATTRIBUTES OCCURS 40 TIMES
INDEXED BY ATRINDEX.
15 WS-ATTR-CD PIC X(01) VALUE SPACES.

01 WS-ARR-MAX-SIZE PIC 9(02)

Code which loads the table
------------------------------------

SET ATRINDEX TO 1
PERFORM A100-POPULATE-ATTR-ARRAY
UNTIL ATRINDEX > WS-ARR-MAX-SIZE
OR END-OF-ATTR-YES

-------------------------------------------

Control is not going to 'A100-POPULATE-ATTR-ARRAY'. But if hard code 40 instead of WS-ARR-MAX-SIZE, the code is working fine. I tried by giving PIC clause 'PIC S9(4) COMP' for WS-ARR-MAX-SIZE. But it did not work. Can anyone here please suggest way to remove the hard coding of 40.

Re: Indexed Array issue

PostPosted: Wed Feb 16, 2011 7:40 pm
by NicC
But it did not work.

In what way did it not work? Have you initialised ws-array-max-size - if so, to what value?

For your supplementary question try looking up OCCURS DEPENDING ON

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 5:30 am
by BillyBoyo
pacha wrote:Can anyone here please suggest way to remove the hard coding of 40.


Why do you want to get rid of the hard coding of 40?

COBOL needs to know how much space to allocate, so needs to know the maximum number of entries possible (even with OCCURS DEPENDING ON, in the old days, anyway).

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 7:33 am
by Robert Sample
Actually, for tables in WORKING-STORAGE, COBOL still allocates space for the maximum number of possible entries in the table -- ODO is more a programming convention than actually impacting memory.

I expect the original problem was not setting the maximum variable value to 40 -- hardcoding the value in the PERFORM would bypass the undefined maximum value and hence work as desired.

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 9:24 am
by pacha
Sorry I missed to mention that in working storage WS-ARR-MAX-SIZE has a value clause as 40 and it was initialized with an initialize statement . Still it is not working.

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 9:27 am
by dick scherrer
Hello,

Why do you want to get rid of the hard coding of 40?


As the pacha said. . .
But if hard code 40 instead of WS-ARR-MAX-SIZE, the code is working fine. I tried by giving PIC clause 'PIC S9(4) COMP' for WS-ARR-MAX-SIZE. But it did not work. Can anyone here please suggest way to remove the hard coding of 40.

The intent was to use the "discovered max" rather than hard-coding the max in the PERFORM. . .

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 9:28 am
by dick scherrer
Hello,

Why was INITIALIZE used? The highest entry used should be placed in the max. . .

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 10:19 am
by pacha
Sorry I confused you
Table
-----------------------------------
01 WS-ATTR-ARRAY.
10 WS-ATTRIBUTES OCCURS 40 TIMES
INDEXED BY ATRINDEX.
15 WS-ATTR-CD PIC X(01) VALUE SPACES.

01 WS-ARR-MAX-SIZE PIC 9(02) VALUE 40.

Code which loads the table
------------------------------------
INITIALIZE WS-ARR-MAX-SIZE
SET ATRINDEX TO 1
PERFORM A100-POPULATE-ATTR-ARRAY
UNTIL ATRINDEX > WS-ARR-MAX-SIZE
OR END-OF-ATTR-YES

In the case mentioned above control is not going to A100-POPULATE-ATTR-ARRAY. the I tried with the below code. it worked fine.

SET ATRINDEX TO 1
PERFORM A100-POPULATE-ATTR-ARRAY
UNTIL ATRINDEX > 40
OR END-OF-ATTR-YES

Here I am sure that END-OF-ATTR-YES is not true. Look like there is an issue with the PIC clause of WS-ARR-MAX-SIZE. is there any restriction that we should use only some specific PIC clause while comapring with an INDEXED varaible? Even i tried with PIC S9(04) COMP. Kindly let me know ur suggestions?

Thanks,
Pacha

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 10:38 am
by dick scherrer
Hello,

You have misunderstood how INITIALIZE works. . . From the manual:
When the REPLACING phrase is not specified:

SPACE is the implied sending item for receiving items of category alphabetic, alphanumeric, alphanumeric-edited, DBCS, national, or national-edited.

ZERO is the implied sending item for receiving items of category numeric or numeric-edited.
Basically your code moved zero to your max field.

However, you really don't want to use 40 (if i understand what this is to do). I believe you really want the highest entry used. For example if there are only 25 entries filled in the array, why set the max to 40?

Re: Indexed Array issue

PostPosted: Thu Feb 17, 2011 10:34 pm
by David Jackson
I am assuming that in his A100-POPULATE-ATTR-ARRAY that he is determining the maximum number of entries to set (if less than 40) and setting
END-OF-ATTR-YES as his Perform statement is covering both situations.

PERFORM A100-POPULATE-ATTR-ARRAY
UNTIL ATRINDEX > 40
OR END-OF-ATTR-YES

but he has not provided the A100- code in order to be sure.
As you say his primary problem was in not setting WS-ARR-MAX-SIZE to 40 so the PERFORM UNTIL simply dropped straight through.
Also if it were me, I would define WS-ARR-MAZ-SIZE as a PIC S9(3) COMP-3 with a value of +40. I would not do the Perform with a hard code value located so far from the initial definition of the table -and remove that INITIALIZE.