multidimensional array setting/slicing?

bearophile bearophileHUGS at lycos.com
Mon Jul 28 08:02:28 PDT 2008


Saaa Wrote:
> ... erm I barely follow it myself :/

There are ways to shorten your code a little, and/or to make it a bit more readable. You have to learn still where to put spaces too.

>  int array[][];
>  array.length=10;
>  foreach(ref a; array)
>   a.length = 5;

There's this syntax too, if you remember the order of the sizes:
auto array = new int[][][](n1, n2, n3);

foreach(ref a; array)
=>
foreach (ref a; array)

> a[1..1+array2[i].length] = array2[i];
=>
> a[1 .. 1+array2[i].length] = array2[i];

array2.length=2;
=>
array2.length = 2;

In some situations you can use $ instead of length (but not in the code you have shown, I think).
And you can write yourself a slice(...) function able to perform your complex slices reducing syntax clutter (but it doesn't support the .. operator, sadly, as in the .../: of python).

Note that this:
auto a = [[1, 2], [3, 4]];
Isn't the same thing as:
int[][] a = [[1, 2], [3, 4]];
If you use auto the compiler chooses to use a static array.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list