> 
> I have a reference to an annonymous array, which I am looping through with
> 
> foreach(@$servref) {
>         if ( checkServer($_, $dn) ) {
>                 push(@$goodservref, $_);
>         } else {
>                 # server wasn't good. Add another item to the list
>                 push(@$servref, newitem);
>         }
> }
> 
> 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? If not,
> what would be a better solution?


>From perldoc perlsyn:

   If any part of LIST is an array, foreach will get very confused if
   you add or remove elements within the loop body, for example with
   splice. So don't do that.

A solution that doesn't violate the "don't do that" rule would be to
use a while loop instead of a for loop.

You can get away with the following, so long as @$servref has no
undefined elements.

while ( my $server = shift @$servref ) { 
 .
 .
 .
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.

-- 
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