How to convert a chunks result to a two-dimensional array

Steven Schveighoffer schveiguy at gmail.com
Sat Jan 15 03:48:41 UTC 2022


On 1/14/22 9:41 PM, Paul Backus wrote:
> On Saturday, 15 January 2022 at 01:49:14 UTC, forkit wrote:
>> I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 
>> 12, 13, 14, 15]]
>>
>> Any help will be appreciated.
>>
>> note: to2Darray is not a valid statement ;-)
>>
>>
>> // ---
>> module test;
>>
>> import std;
>>
>> void main()
>> {
>>     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // how to 
>> convert this into [][]
>> }
>> // ---
> 
> import std.algorithm: map;
> import std.array: array;
> 
> int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array;

Alternatively (with only one allocation for the int[] data):

```d
int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array;
```

-Steve


More information about the Digitalmars-d-learn mailing list