2-dimensional array confusion

Andreas Kaempf andreas.kaempf at web.de
Mon Nov 1 00:50:12 PDT 2010


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


More information about the Digitalmars-d-learn mailing list