iota access in foreach loop

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 4 11:20:26 PDT 2016


Hi all!
Could you help me clearify why a iota can't be accessed with two 
arguments in a foreach loop?
following tests show my problem:
What does work:
     int[] ku = [0, 1, 2, 3, 4];
     foreach(i, el; ku)
         writeln("index: ", i, " element: ", el);
What does not work:
     counter = 5;
     foreach(i, el; iota(counter))
         writeln("index: ", i, " element: ", el);

Motivation: In real I want to have:
     counter = 5;
     foreach(i, el; randomCover(iota(counter)))
         writeln("index: ", i, " element: ", el);

so, I could follow a random permutation. Maybe there is another 
simple way to achieve this?

PS: needed imports:
     import std.random : randomCover;
     import std.range : iota;
     import std.stdio : writeln;


More information about the Digitalmars-d-learn mailing list