VladislavShcherba
VladislavShcherba

Reputation: 460

Jagged array PL1 (PL/I)

I need jagged dynamic array in PL/I but I don't know how to realize it. Need something like:

dcl v dim(*,*) fixed bin(31) ctl;
allocate v dim(5,*);             
allocate v(1) dim(10);           
allocate v(2) dim(100);    

How can I do it?

Upvotes: 0

Views: 1333

Answers (2)

VladislavShcherba
VladislavShcherba

Reputation: 460

I solved this this problem in a way that piet.t had told. Have created array of pointers and allocate arrays for each pointer. This looks not very convenient IMHO.

    mainp:procedure options(main);                                 

    dcl n fixed bin(31);                                           
    dcl pp ptr;                                                    
    dcl 1 pointers based(pp),                                      
          2 length fixed bin(31),                                  
          2 at dim(n refer (length)) ptr;                          

    dcl m fixed bin(31);                                           
    dcl 1 values based,                                            
          2 length fixed bin(31),                                  
          2 at dim(m refer (length)) fixed bin(31);                

    n=2;                                                           
    allocate pointers;                                             
    m=2;                                                           
    allocate values set(pointers.at(1));                           
    m=4;                                                           
    allocate values set(pointers.at(2));                           

    pointers.at(1)->values.at(1)=1;                                
    pointers.at(1)->values.at(2)=2;                                
    pointers.at(2)->values.at(1)=10;                               
    pointers.at(2)->values.at(2)=20;                               
    pointers.at(2)->values.at(3)=30;                               
    pointers.at(2)->values.at(4)=40;                               

    put skip list("Line 1 length: ",pointers.at(1)->values.length);
    put skip list("Line 2 lenght: ",pointers.at(2)->values.length);
    put skip list('');                                             
    put skip list(pointers.at(1)->values.at(1),                    
                  pointers.at(1)->values.at(1));                   

    put skip list(pointers.at(2)->values.at(1),                    
                  pointers.at(2)->values.at(2),                    
                  pointers.at(2)->values.at(3),
              pointers.at(2)->values.at(4));                               
end; 

Upvotes: 0

zarchasmpgmr
zarchasmpgmr

Reputation: 1424

An asterisk in an array declaration is used only in a subroutine, and indicates that the array dimensions will be taken from the metadata of the array passed in the argument.

For controlling the size of the array allocated via the allocate statement, use variables:

dcl v dim(d1, d2) ctl, 
   d1 fixed bin(31), 
   d2 fixed bin(31); 

 d1 = 5; 
 d2 = 15; 
 alloc v;

This will give you v(5, 15).

You may use variables for both lower and upper bounds:

dcl w dim(l1:u1, l2:u2) ctl, 
   l1 fixed bin(31), 
   l2 fixed bin(31),
   u1 fixed bin(31), 
   u2 fixed bin(31); 

 get data(l1, u1, l2, u2);
 alloc w;

With SYSIN input of l1=-4; u1=6; l2=15; u2=144;, w is allocated as w(-4:6, 15:144).

For IBM PL/I, see the V5.1 Language Reference manual section Controlled storage and attribute, or page 260 (real page 296) of the current PDF document.

Upvotes: 0

Related Questions