Iterate over an array while mutating it?

bearophile bearophileHUGS at lycos.com
Thu Mar 27 15:27:17 PDT 2014


Anh Nhan:

> I want to iterate over an array, while adding new entries, and 
> have those in the iteration loop.

Don't iterate an array with foreach while you mutate it, even if 
you manage to make it work, the code is not clear.
I suggest to use a C-style for loop with an index and specify in 
the code exactly what you want to do with the array and the 
index. The resulting code is clear and safe.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list