get element index when using each!(x)

dangbinghoo dangbinghoo at google-mail.com
Thu Sep 17 05:33:10 UTC 2020


On Thursday, 17 September 2020 at 03:16:42 UTC, Paul Backus wrote:
> 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

thanks you! and thanks to JG.

---------
binghoo


More information about the Digitalmars-d-learn mailing list