get element index when using each!(x)

Paul Backus snarwin at gmail.com
Thu Sep 17 03:16:42 UTC 2020


On Thursday, 17 September 2020 at 03:14:08 UTC, JG wrote:
>
> 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]));
> }

Worth knowing that the tuples you get from enumerate actually 
have named members, so you can write:

     s.enumerate.each!(x => writeln(x.index, ":", x.value));

Documentation: 
http://dpldocs.info/experimental-docs/std.range.enumerate.html


More information about the Digitalmars-d-learn mailing list