On Thursday, 17 September 2020 at 00:51:54 UTC, dangbinghoo wrote:
hi,
is there any way to get the index for an element when iteration
using each!(x)?
I know I can do this using foreach statement, but I prefer
using the each template.
-----------
string s = "hello";
foreach(i, c; s) {
}
----------
how can I get to ?
Thanks!
binghoo dang
Perhaps there are other ways, but you can use enumerate. For
example
-----------
import std.algorithm;
import std.range;
import std.stdio;
void main() {
string s = "hello";
s.enumerate.each!(x=>writeln(x[0],":",x[1]));
}
-----------
Produces
-----------
0:h
1:e
2:l
3:l
4:o
-----------