Default value for new dimension in redimension


#1

I use redimension to add a new dimension to an array. How can I set a default value for the new dimension?

I start from this:

AFL% build(<val:int8>[i=1:3,3,0], i);
{i} val
{1} 1
{2} 2
{3} 3

And after redimension I get this:

AFL% redimension(
       build(<val:int8>[i=1:3,3,0], i),
       <val:int8>[i=1:3,3,0, j=1:10,10,0]);
{i,j} val
{1,1} 1
{2,1} 2
{3,1} 3

What I would like to get instead is:

{i,j} val
{1,5} 1
{2,5} 2
{3,5} 3

I managed to get what I wanted using:

AFL% redimension(
       redimension(
         build(<val:int8>[i=1:3,3,0], i),
         <val:int8>[i=1:3,3,0, j=5:5,10,0]),
       <val:int8>[i=1:3,3,0, j=1:10,10,0]);
{i,j} val
{1,5} 1
{2,5} 2
{3,5} 3

Is this the right way or is there a better way?


#2

The best way is to use apply() to add a new attribute with the default dimension value, and then use redimension() to turn that attribute into a dimension:

AFL% redimension(
       apply(build(<val:int8>[i=1:3,3,0], i),
               j, 5),
       <val:int8>[i=1:3,3,0,j=1:10,10,0]);
{i,j} val
{1,5} 1
{2,5} 2
{3,5} 3

The apply() is much faster than the second redimension()!