Multidimensional arrays, foreach loops and slices

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Wed May 30 09:15:10 PDT 2012


Hello all,

A couple of queries.  The first I'm sure has been asked/answered 
definitively before, but a search doesn't bring anything up: is 
it possible to foreach() over every element of a multidimensional 
array, i.e. something like,

     int[3][4][5] a;

     foreach(ref int n; a)
         n = uniform!"[]"(0, 1);

... or is it necessary to do a separate foreach over each 
dimension?

The second query relates to slices.  Suppose I have an N*M 2d 
array (let's call it a), and a 1d array of length N (let's call 
it s) whose elements are unsigned integers whose values fall in 
[0, M).  What I'd like is to take a slice of a such that I get 
out a 1d array of length N such that the i'th element corresponds 
to a[s[i]].

Is this possible?  And if so, how is it achieved?

Thanks and best wishes,

     -- Joe


More information about the Digitalmars-d-learn mailing list