D slicing

Colin Grogan grogan.colin at gmail.com
Tue Jun 18 01:13:43 PDT 2013


On Monday, 17 June 2013 at 23:48:36 UTC, Ali Çehreli wrote:
> On 06/17/2013 04:34 PM, Colin Grogan wrote:
>
> > Wondering what way I'd go about this, I want to slice an
> array into two
> > arrays. First array containing every even index (i.e.
> 0,2,4,6,8..$)
> > Second slice containing every odd index (i.e. 1,3,5,7,9..$)
> <-- be some
> > issue with using $ depending on if orig length is odd or
> even. Can work
> > that out easily enough...
> >
> > Reading the articles on array slicing its not clear if its
> possible.
> >
> > Ideally, I could do something like the following:
> >
> > auto orig = [1,2,3,4,5,6,7];
> > auto sliceEven = orig[0..$..2];
> > auto sliceOdd = orig[1..$..2];
>
> If you want the data sit where it is but simply have different 
> views in it, then you must use ranges. There are multiple ways.
>
> Here is one using std.range.stride:
>
> import std.stdio;
> import std.range;
>
> void main()
> {
>     auto orig = [0, 1, 2, 3, 4, 5, 6, 7];
>
>     auto sliceEven = orig.stride(2);
>     auto sliceOdd = orig.dropOne.stride(2);
>
>     writeln(sliceEven);
>     writeln(sliceOdd);
> }
>
> The output:
>
> [0, 2, 4, 6]
> [1, 3, 5, 7]
>
> Or you can generate the indexes and then get a view that way:
>
>     auto sliceEven = orig.indexed(iota(0, orig.length, 2));
>     auto sliceOdd = orig.indexed(iota(1, orig.length, 2));
>
> Ali

Thats perfect folks. Should have known to look in std.range.
This works for me perfectly.

3 answers all within a couple minutes of each other, what a good 
community! ;)


More information about the Digitalmars-d-learn mailing list