get element index when using each!(x)
JG
someone at somewhere.com
Thu Sep 17 03:14:08 UTC 2020
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
-----------
More information about the Digitalmars-d-learn
mailing list