Subarrays of arrays

bearophile bearophileHUGS at lycos.com
Sun Dec 30 04:51:57 PST 2012


Omid:

> arr = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
>
> I want to obtain a subarray like
>
> assert(arr[][0] == [1,7,13]);

This gives a range, not an array, so if you want an array, you 
have to call std.array.array later:

http://dlang.org/phobos/std_range.html#transversal


> assert(arr[1][] == [7,8,9,10,11,12];

Just use array[1]. Or array[1].dup is you want a copy.


> assert(arr[0,2][1] == [2,14]);

Enumerated indexes are not supported, so you need a iota + filter 
+ transversal + array.


> assert(arr[0,2][0..3,5] == [[1,2,3,6],[13,14,15,18]]);

This requires a iota + filter + chain + array, or something like 
that.

If you don't want to do all this, you have to write your slicing 
and dicing array struct :-( Someday it will probably be present 
in Phobos.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list