foreach: How start a foreach count with specific number?

Paul Backus snarwin at gmail.com
Wed Jun 2 16:27:54 UTC 2021


On Wednesday, 2 June 2021 at 15:49:36 UTC, Marcone wrote:
> But I don't want it starts with 0, but other number. How can I 
> do it?

Easiest way is to just add the starting number:

     size_t start = 5;
     foreach (n, i; glob("*")) {
         print("{} DATA {}".format(n, start + i));
     }

You can also use [`std.range.enumerate`][1], which takes the 
number to start with as an optional argument:

     foreach (n, i; glob("*").enumerate(5)) {
         print("{} DATA {}".format(n, start + i));
     }

[1]: https://phobos.dpldocs.info/std.range.enumerate.html


More information about the Digitalmars-d-learn mailing list