0

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?

zer_ik
  • 410
  • 1
  • 4
  • 14
  • There seems to be no way to do this in the way you want. If you really need somthing like this you perhaps should allocate an array of pointers pointing to the separate arrays of the second dimension. – piet.t Jun 14 '17 at 06:34
  • @piet.t Thank you. I answered the qestion in a way you have told. May be this code can be less complicated though... – zer_ik Jun 19 '17 at 10:52

2 Answers2

0

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.

zarchasmpgmr
  • 1,422
  • 10
  • 21
  • You can use the asterisk in the definition of a `controlled` variable and specify the actual dimension at allocation-time. But as I see it you can only allocate the array as a whole (by specifying all two dimensions at once). So defining a jagged array this way won't work. – piet.t Jun 14 '17 at 06:32
  • Reading the Language Guide, it doesn't read that way. But then again we are talking IBM documentation. – zarchasmpgmr Jun 14 '17 at 15:07
0

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; 
zer_ik
  • 410
  • 1
  • 4
  • 14