www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array operations with multidimensional arrays

reply Marduk <mardukbp mac.com> writes:
In the documentation one can learn how to do array operations 
with 1D arrays. However, this does not scale up for 2D arrays. 
For example, the following does not work:

int[2][2] a,b;
a = [[1,1],[1,1]];
b[][] = a[][]*2;


Additionally, I would like to assign 2D sub-arrays of a 3D array, 
i.e. something like the following:

int[3][2][2] a;

a[0] = [[2,2], [2,2]];


I did not understand how to use std.experimental.ndslice to do 
this. An example would be greatly appreciated.
Nov 19 2016
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote:
 Additionally, I would like to assign 2D sub-arrays of a 3D 
 array, i.e. something like the following:

 int[3][2][2] a;

 a[0] = [[2,2], [2,2]];
You have the dimensions the wrong way around. a is a 2 element array of 2 element arrays of 3 element arrays. int[3][2][2] a; a[0] = [[2,2,2], [2,2,2]]; works fine.
Nov 19 2016
parent reply Marduk <mardukbp mac.com> writes:
On Saturday, 19 November 2016 at 17:37:58 UTC, John Colvin wrote:
 On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote:
 Additionally, I would like to assign 2D sub-arrays of a 3D 
 array, i.e. something like the following:

 int[3][2][2] a;

 a[0] = [[2,2], [2,2]];
You have the dimensions the wrong way around. a is a 2 element array of 2 element arrays of 3 element arrays. int[3][2][2] a; a[0] = [[2,2,2], [2,2,2]]; works fine.
Thanks a lot! Now I get what it means that array declarations are read from right to left.
Nov 19 2016
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 19 November 2016 at 19:36:50 UTC, Marduk wrote:
 On Saturday, 19 November 2016 at 17:37:58 UTC, John Colvin 
 wrote:
 On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote:
 Additionally, I would like to assign 2D sub-arrays of a 3D 
 array, i.e. something like the following:

 int[3][2][2] a;

 a[0] = [[2,2], [2,2]];
You have the dimensions the wrong way around. a is a 2 element array of 2 element arrays of 3 element arrays. int[3][2][2] a; a[0] = [[2,2,2], [2,2,2]]; works fine.
Thanks a lot! Now I get what it means that array declarations are read from right to left.
The way I think about it is this: int is a type. int[3] is an array of 3 ints. Similarly, int[3] is a type, so an array of 2 int[3]s is int[3][2] and so on...
Nov 19 2016
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Saturday, 19 November 2016 at 21:05:49 UTC, John Colvin wrote:
 On Saturday, 19 November 2016 at 19:36:50 UTC, Marduk wrote:
 Thanks a lot! Now I get what it means that array declarations 
 are read from right to left.
The way I think about it is this: int is a type. int[3] is an array of 3 ints. Similarly, int[3] is a type, so an array of 2 int[3]s is int[3][2] and so on...
A while back I was writing a Sudoku solver which used static array types. It went something like this: alias Possible = byte[10]; //1-9 possible, plus final known value alias Block = Possible[9]; alias Sudoku = Block[9]; Actual Sudoku: byte[10][9][9] While this breaks down easily enough, if the order had been the other way around it wouldn't have been extensible this way to making larger structures from basic types/arrays.
Nov 19 2016