www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 2-dimensional array confusion

reply Andreas Kaempf <andreas.kaempf web.de> writes:
Hey folks!

Please enlight me with that prefix notation of 2-dimensional arrays! I
prepared a snippet giving me headaches:

auto int[2][3] test = [[11,12],[21,22],[31,32]];
foreach (x, row; test)
{
	Stdout.format("x={}: ", x+1);

	foreach (y, cell; row)
	{
		Stdout.format("{}:({}) ", y+1, test[x][y]);
//		Stdout.format("{}:({}) ", y+1, test[y][x]); // I expected this here
according the declariation!
	}
	Stdout.newline;
}

According to the documentation, the declaration of test should declare 3
arrays of two ints. The initialization works fine so that's ok for me.

But why do I have to access it with test[x][y] to produce this result?

x=1: y=1:(11) y=2:(12)
x=2: y=1:(21) y=2:(22)
x=3: y=1:(31) y=2:(32)

This literally drives me crazy! I really want to understand that!

Your help is highly appreciated!

Thanks a lot!

Andreas
Nov 01 2010
next sibling parent spir <denis.spir gmail.com> writes:
On Mon, 1 Nov 2010 07:50:12 +0000 (UTC)
Andreas Kaempf <andreas.kaempf web.de> wrote:

 According to the documentation, the declaration of test should declare 3
 arrays of two ints. The initialization works fine so that's ok for me.
=20
 But why do I have to access it with test[x][y] to produce this result?
Yes, this is a bit mind disturbing. If you consider each syntax independant= ly, they are both completely logical: * type def: since T[2] defines an array of 2 T's, then T[2][3] well defines= an array of 3 arrays of 2 T's. * element access: as your literal [[11,12],[21,22],[31,32]] well shows, the= 2-element arrays are the nested ones; so that, to access an element, one m= ust first access a 2-element array --> arr[2-element-array-index][element-index] which maps to arr[row-index][element-index] as one expects. But the confrontation of both logics is somewhat troubling ;-) To solve it,= one would need to reverse the=20 array definition format: [n]T means array of n T's, [m][n]T means array of = m arrays of n T's. Simply forget about it and use each syntax according to its own point if vi= ew. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 01 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 01 Nov 2010 03:50:12 -0400, Andreas Kaempf <andreas.kaempf web.de>  
wrote:

 Hey folks!

 Please enlight me with that prefix notation of 2-dimensional arrays! I
 prepared a snippet giving me headaches:

 auto int[2][3] test = [[11,12],[21,22],[31,32]];
 foreach (x, row; test)
 {
 	Stdout.format("x={}: ", x+1);

 	foreach (y, cell; row)
 	{
 		Stdout.format("{}:({}) ", y+1, test[x][y]);
 //		Stdout.format("{}:({}) ", y+1, test[y][x]); // I expected this here
 according the declariation!
 	}
 	Stdout.newline;
 }

 According to the documentation, the declaration of test should declare 3
 arrays of two ints. The initialization works fine so that's ok for me.

 But why do I have to access it with test[x][y] to produce this result?

 x=1: y=1:(11) y=2:(12)
 x=2: y=1:(21) y=2:(22)
 x=3: y=1:(31) y=2:(32)

 This literally drives me crazy! I really want to understand that!
test is a 3-element array of 2-element arrays. When you index test, you get a 2-element array as its element. When you index that, you get the individual ints. Think about it in terms of a single array. If you have an array of type T[3], it's element type is T. So if T is int[2], then it appears perfectly consistent: int[2][3] t; // T is int[2] auto x = t[0]; // T x is of type int[2] As an aside, your foreach code is mighty inefficient. Each time through the array, row is a copy of the original element, which means you are moving a lot of data without ever using it. I'd recommend using ref, like this: foreach(x, ref row; test) Unfortunately, you can't use the 0..test.length syntax since you are using D1. You could use a straight for-loop. -Steve
Nov 02 2010