On 10/31/06, Andy Greenwood <[EMAIL PROTECTED]> wrote:

I have a reference to an annonymous array, which I am looping through with

foreach(@$servref) {

Based on certain criteria, I want to add a new item to the end of the
$servref array. Is it safe to do this inside the foreach loop?

Nope; as your intuition suggested, it's bad to modify the variable
while looping over it.

If not, what would be a better solution?

Perhaps collect any new elements into another array, then push them
onto the main array after the loop ends. But maybe there's a way to
rethink your algorithm, especially if you want to re-check the new
elements. (Maybe one array of unchecked items, and another array of
ones that are already checked?)

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to