Re: Iterate over an array while mutating it?

2014-03-28 Thread Regan Heath
On Thu, 27 Mar 2014 22:23:40 -, Anh Nhan wrote: Hey guys, I want to iterate over an array, while adding new entries, and have those in the iteration loop. See here: https://gist.github.com/AnhNhan/9820226 The problem is that the foreach loop seemingly only iterates over the original

Re: Iterate over an array while mutating it?

2014-03-28 Thread bearophile
Anh Nhan: Ah, that the foreach caches the entries would make sense. What does it mean "foreach caches the entries" for you? Foreach does very little. It is light syntax sugar over a normal for loop. Bye, bearophile

Re: Iterate over an array while mutating it?

2014-03-28 Thread Marc Schütz
On Thursday, 27 March 2014 at 22:23:41 UTC, Anh Nhan wrote: Hey guys, I want to iterate over an array, while adding new entries, and have those in the iteration loop. See here: https://gist.github.com/AnhNhan/9820226 The problem is that the foreach loop seemingly only iterates over the orig

Re: Iterate over an array while mutating it?

2014-03-27 Thread Anh Nhan
On Thursday, 27 March 2014 at 22:26:37 UTC, Infiltrator wrote: On Thursday, 27 March 2014 at 22:23:41 UTC, Anh Nhan wrote: I want to iterate over an array, while adding new entries, and have those in the iteration loop. Depending on what you're trying to do, perhaps you would be better off ju

Re: Iterate over an array while mutating it?

2014-03-27 Thread Infiltrator
On Thursday, 27 March 2014 at 22:23:41 UTC, Anh Nhan wrote: I want to iterate over an array, while adding new entries, and have those in the iteration loop. Depending on what you're trying to do, perhaps you would be better off just adding the entries all at once instead of iterating the arra

Re: Iterate over an array while mutating it?

2014-03-27 Thread bearophile
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

Iterate over an array while mutating it?

2014-03-27 Thread Anh Nhan
Hey guys, I want to iterate over an array, while adding new entries, and have those in the iteration loop. See here: https://gist.github.com/AnhNhan/9820226 The problem is that the foreach loop seemingly only iterates over the original array, not minding the newly added entries. Does someb