Indexed Array issue



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Indexed Array issue

Postby pacha » Wed Feb 16, 2011 6:51 pm

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.
pacha
 
Posts: 10
Joined: Sun Mar 21, 2010 10:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Indexed Array issue

Postby NicC » Wed Feb 16, 2011 7:40 pm

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
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Indexed Array issue

Postby BillyBoyo » Thu Feb 17, 2011 5:30 am

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).
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Indexed Array issue

Postby Robert Sample » Thu Feb 17, 2011 7:33 am

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Indexed Array issue

Postby pacha » Thu Feb 17, 2011 9:24 am

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.
pacha
 
Posts: 10
Joined: Sun Mar 21, 2010 10:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Indexed Array issue

Postby dick scherrer » Thu Feb 17, 2011 9:27 am

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. . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Indexed Array issue

Postby dick scherrer » Thu Feb 17, 2011 9:28 am

Hello,

Why was INITIALIZE used? The highest entry used should be placed in the max. . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Indexed Array issue

Postby pacha » Thu Feb 17, 2011 10:19 am

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
pacha
 
Posts: 10
Joined: Sun Mar 21, 2010 10:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Indexed Array issue

Postby dick scherrer » Thu Feb 17, 2011 10:38 am

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?
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Indexed Array issue

Postby David Jackson » Thu Feb 17, 2011 10:34 pm

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.
User avatar
David Jackson
 
Posts: 9
Joined: Wed Jan 26, 2011 5:09 am
Location: California
Has thanked: 0 time
Been thanked: 0 time

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post