foreach with assoc. array

Salih Dincer salihdb at hotmail.com
Thu Mar 2 10:34:14 UTC 2023


On Wednesday, 1 March 2023 at 19:05:10 UTC, DLearner wrote:

> ```
> Error: variable `wk_Idx` is shadowing variable 
> `for3.main.wk_Idx`
> ```
> Why is this usage wrong?

Or use the `each` template which is almost the same as `foreach` 
to avoid the shadowing variable issue.

```d
import std.algorithm, std.range, std.stdio;

enum len = 10;
void main()
{
   int[len] IntArr, i, n;
   len.iota.each!((i, n) => IntArr[i] = n);

   IntArr.writeln; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   assert(IntArr == len.iota.array);
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list