foreach( i, e; a) vs ndslice

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 18 15:33:53 PST 2016


I'm playing with the example below.  I noticed a few things.
1. The ndslice didn't support the extra index, i, in the foreach, 
so had to add extra i,j.
2. I couldn't figure out a way to use sliced on the original 'a' 
array.  Is slicing only available on 1 dim arrays?
3. Sliced parameter order is different than multi-dimension array 
dimension declaration.

import std.stdio;
import std.experimental.ndslice.slice;

void main() {
     int[4][5] a = new int[20];
     foreach(i,ref r; a){
         foreach(j,ref c; r){
             c= i+j;
             writefln("a(%d,%d)=%s",i,j,c);
         }
     }
     writefln("a=%s",a);

     auto b = new int[20].sliced(5,4);

     int i=0;
     foreach( ref r; b){
         int j=0;
         foreach( ref c; r){
             c= i+j;
             writefln("b(%d,%d)=%s",i,j,c);
             j++;
         }
         i++;
     }
     writefln("b=%s",b);

}


More information about the Digitalmars-d-learn mailing list