Dynamic 2 dimensional array

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

Dynamic 2 dimensional array

Postby Ovia » Sat Sep 11, 2010 7:55 pm

Hi All,

I have a table declartion as follows:
01 EMPLOYEE-TABLE.                                 
 05 EMPLOYEE-COUNT               PIC 9(03).         
 05 EMPLOYEE-TBL   OCCURS 999 TIMES               
                       DEPENDING ON EMPLOYEE-COUNT   
                       INDEXED BY EMPLOYEE-COUNT-IDX.
    10 EMPLOYEE-NM            PIC X(30).         
    10 DEPARTMENT-CNT          PIC 9(03).         
    10 DEPT-TBL OCCURS 1 TO 20 TIMES           
                       DEPENDING ON DEPT-CNT   
                       INDEXED BY DEPT-IDX.     
       15 DEPT-NAME           PIC X(30).   

I have also defined DEPT-CNT in Working storage as 01 DEPT-CNT PIC 9(03).

When I try to populate this 2 dimensional array, I am getting over lay problems.

My piece of code to populate this array is:

ADD 1                                                 TO EMPLOYEE-COUNT               
SET DEPT-IDX                                   TO 1.                     
SET DATA-IDX                                 TO 1.                     
                                                             TO EMPLOYEE-NM (EMPLOYEE-COUNT-IDX).         
                                                             TO DEPARTMENT-CNT (EMPLOYEE-COUNT-IDX).       
INITIALIZE DEPT-CNT    .                                 
PERFORM UNTIL DEPT-CNT >= DATA-CNT                             
      MOVE DATA-DEPT-NM(DATA-IDX)        TO   DEPT-NAME(DEPT-IDX)           
      ADD 1                      TO DEPT-CNT 
       SET DEPT-IDX        UP BY 1           
       SET DATA-IDX       UP BY 1

The Dept count varies from employee to employee

For EG: Empl 1 can handle 2 dept and emp 2 can handle 4 depts.

Let me know where I am going wrong. Thanks!!

Posts: 2
Joined: Tue Apr 27, 2010 3:52 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Dynamic 2 dimensional array

Postby dick scherrer » Sat Sep 11, 2010 9:43 pm


When posting code, jcl, data, etc it is best to use the "Code" tag (your post has been "Code'd").

When I try to populate this 2 dimensional array, I am getting over lay problems.
Explain what is getting "overlayed".
Hope this helps,
User avatar
dick scherrer
Global moderator
Posts: 6269
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Dynamic 2 dimensional array

Postby Robert Sample » Sun Sep 12, 2010 6:45 am

One obvious problem: if you define the number of departments based on a counter that is not under the employee structure, you must manually change the variable DEPT-CNT for each employee or you'll be accessing data that is undefined or overlay existing data.
Robert Sample
Global moderator
Posts: 3686
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 274 times

Return to IBM Cobol


  • Related topics
    Last post