digitalmars.D.learn - how to use unknown size of array at compile time for further
- thorstein (47/47) Oct 01 2017 Hi,
- user456 (2/7) Oct 01 2017 They are not alternatives. They are the only way of doing things.
- thorstein (46/48) Oct 01 2017 Yes, sounds logic - static arrays require a size before
- thorstein (2/4) Oct 01 2017 Guyes, I obviously made a mistake. Forget my post!
- thorstein (49/50) Oct 01 2017 Sorry, I'm still really confused with the results from my
- Ilya Yaroshenko (9/13) Oct 01 2017 Replace
- thorstein (3/11) Oct 01 2017 Works! Thanks Ilya. I'll take a look on the difference. Also soon
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; }
Oct 01 2017
On Sunday, 1 October 2017 at 10:07:40 UTC, thorstein wrote: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. [...]They are not alternatives. They are the only way of doing things.
Oct 01 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!
Oct 01 2017
On Sunday, 1 October 2017 at 13:53:57 UTC, thorstein wrote:Why is that???? Thanks!Guyes, I obviously made a mistake. Forget my post!
Oct 01 2017
Guyes, I obviously made a mistake. Forget my post!Sorry, I'm still really confused with the results from my function: double[][] transp(double[][] array) { double[][] arrayT; double[] rowT; // initialize rowT foreach(i; 0..array.length) { rowT ~= 0; } foreach(col; 0..array[0].length-3) { foreach(row; 0..array.length) { rowT[row] = array[row][col]; } writeln("rowT 1: ",rowT); writeln("arrayT 1: ", arrayT); arrayT ~= rowT; writeln("rowT 2: ",rowT); writeln("arrayT 2: ", arrayT,"\n"); } return arrayT; } Again, 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] ]; ... the results are strange. Before the appending (arrayT ~= rowT;) happens for the second column of array arrayT was miraculous modified: rowT 1: [4, 19, 62, 23, 5, 5] arrayT 1: [] rowT 2: [4, 19, 62, 23, 5, 5] arrayT 2: [[4, 19, 62, 23, 5, 5]] rowT 1: [3, 34, 1, 6, 87, 87] arrayT 1: [[3, 34, 1, 6, 87, 87]] rowT 2: [3, 34, 1, 6, 87, 87] arrayT 2: [[3, 34, 1, 6, 87, 87], [3, 34, 1, 6, 87, 87]] etc... Is there any linkage between rowT and arrayT???? Thanks again!
Oct 01 2017
On Sunday, 1 October 2017 at 14:23:54 UTC, thorstein wrote:Replace arrayT ~= rowT; with arrayT ~= rowT.dup; Also, you may want to look into ndslice package [1]. [1] https://github.com/libmir/mir-algorith Best regards, Ilya Yaroshenko[...]Sorry, I'm still really confused with the results from my function: [...]
Oct 01 2017
Replace arrayT ~= rowT; with arrayT ~= rowT.dup; Also, you may want to look into ndslice package [1]. [1] https://github.com/libmir/mir-algorithm Best regards, Ilya YaroshenkoWorks! Thanks Ilya. I'll take a look on the difference. Also soon will get bit familiar with the mir-algorithm... Best, Torsten
Oct 01 2017