Search
How to convert a chunks result to a two-dimensional array
6 days ago
forkit
5 days ago
Paul Backus
5 days ago
forkit
5 days ago
forkit
5 days ago
Salih Dincer
5 days ago
H. S. Teoh
```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 [][]
}
// ---
```
```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;
```
```On Saturday, 15 January 2022 at 02:41:08 UTC, Paul Backus wrote:
>
> import std.algorithm: map;
> import std.array: array;
>
> int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array;

oh. thanks!

also it seems I reposted the same question (didn't realise this one got posted)
```
```On Sat, Jan 15, 2022 at 01:49:14AM +0000, forkit via Digitalmars-d-learn wrote: [...]
>     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // how to
> convert this into [][]
[...]

auto arrayOfArrays = iota(1, 16).chunks(5).map!(r => r.array).array;

T

--
Winners never quit, quitters never win. But those who never quit AND never win are idiots.
```

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):

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

-Steve

```On Saturday, 15 January 2022 at 03:48:41 UTC, Steven Schveighoffer wrote:
>
> Alternatively (with only one allocation for the int[] data):
>
> ```d
> int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array;
> ```
>
> -Steve

But I like this one the best, because I find it both easier to remember, and easier to expand upon (i.e. for each extra [] just add another .chunks(val).array

e.g

int[][][] arrayOfArraysV4 = iota(1, 16).array.chunks(2).array.chunks(3).array;

int[][][][] arrayOfArraysV5 = iota(1, 16).array.chunks(2).array.chunks(3).array.chunks(4).array;

```

On Saturday, 15 January 2022 at 06:43:05 UTC, forkit wrote:

>

On Saturday, 15 January 2022 at 03:48:41 UTC, Steven Schveighoffer wrote:

>

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

-Steve