to! converting 1D to 2D array

bearophile bearophileHUGS at lycos.com
Tue Mar 11 19:14:44 PDT 2014


ed:

> I am trying to convert a 1D array to a 2D array

If you have a dynamic array (1D), you can convert it to a dynamic 
array of dynamic arrays (2D) using chunks:


void main() {
     import std.stdio, std.range, std.algorithm;

     int[] a = [1, 2, 3, 4, 5, 6];
     int[][] b = a.chunks(2).array;
     b.writeln;
}


Output:

[[1, 2], [3, 4], [5, 6]]

Your problems are caused by mixing fixed-size arrays (that are 
values, allocated in-place), with dynamic arrays (that are little 
length+pointer structs that often point to heap-allocated memory).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list