how to use unknown size of array at compile time for further processing

thorstein torsten.lange at mail.de
Sun Oct 1 10:07:40 UTC 2017


Hi,
assumed, I need the size of a dynamic array for further 
processing, which is unknown at compile time. Below are my 
example, which doesn't work and two alternatives.

Alternative1: may create large rowT-arrays depending on original 
array size.
Alternative2: overrides rowT after exiting the inner loop (which 
may be costly?)

How could I implement something like the first approach?

Thanks!

// Not working example of transposing a matrix
double[][] transp(double[][] array)
{
   double[][] arrayT;
   double[array[0].length] rowT;
   foreach(col; 0..array[0].length)
   {
     foreach(row; array)
     {
       rowT[col]= row[col];
     }
     arrayT ~= rowT;
   }
   return arrayT;
}

// Alternative1
   double[][] arrayT;
   double[] rowT;
   foreach(col; 0..array[0].length)
   {
     foreach(row; array)
     {
       rowT ~= row[col];
     }
     arrayT ~= rowT[$-6..$];
   }

// Alternative2
   double[][] arrayT;
   foreach(col; 0..array[0].length)
   {
     double[] rowT;
     foreach(row; array)
     {
       rowT ~= row[col];
     }
     arrayT ~= rowT;
   }


More information about the Digitalmars-d-learn mailing list