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

thorstein torsten.lange at mail.de
Sun Oct 1 13:53:57 UTC 2017


> They are not alternatives. They are the only way of doing 
> things.

Yes, sounds logic - static arrays require a size before 
compilation.
However, I tried another variation with a totally unexpected 
result:

double[][] transp3(double[][] array)
{

   double[][] arrayT;
   double[] rowT;
   // initialize rowT
   foreach(i; 0..array.length)
   {
     rowT ~= 0;
   }

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

     writeln("rowT: ",rowT);
     writeln("arrayT: ", arrayT);
   }
     return arrayT;
}

Assuming my original array to be:
   array =
     [
       [ 4,  3,   1,   5,  2],
       [19, 34,  23, 100, 59],
       [62,  1,   4,   0, 76],
       [23,  6, 989,  98,  1],
       [ 5, 87,  45,  62,  9],
       [ 5, 87,  45,  62,  9]
     ];

...then the printed result is unexpectedly wrong. The transposed 
columns (rowT) are correct, but arrayT ~= rowT doesn't only 
append rowT to arrayT. It overrides also the previous rows with 
the new transposed column:
rowT: [4, 19, 62, 23, 5, 5]
arrayT: [[4, 19, 62, 23, 5, 5]]
rowT: [3, 34, 1, 6, 87, 87]
arrayT: [[3, 34, 1, 6, 87, 87], [3, 34, 1, 6, 87, 87]]
etc...

Why is that????

Thanks!



More information about the Digitalmars-d-learn mailing list