Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The question then is is this violation of the general prohibition safe or was I lucky
Now I am confused. What do you expect,
  1. Perl loops over the original array, whatever you do to the array in the meantime
  2. Perl loops over the modified array
Frankly, I have no idea what to expect, you can make a case for both behaviors. In such a case, I'd say: don't do it. What will happen in this case might change over time, possibly because someone decided to fix perl's "erratic behavior".

What I think is safe, is this:

  1. Loop over the original array: make a temporary copy.
    foreach(my @temp = @arr) { push @arr, 'd' if $_ eq 'a'; push @arr, 'e' if $_ eq 'b'; push @arr, 'f' if $_ eq 'c'; print $_; }
  2. if you want to loop over the modified array, use the loop index:
    for(my $i = 0; $i < @arr; $i++) { local $_ = $arr[$i]; push @arr, 'd' if $_ eq 'a'; push @arr, 'e' if $_ eq 'b'; push @arr, 'f' if $_ eq 'c'; print $_; }
p.s. I was expecting this to work:
foreach(() = @arr) { print; }
but it doesn't loop even once. I think that's a bit weird, since
$x = () = @arr;
actually sets $x to the size of the array.

In reply to Re: Is it safe to append to the array you are iterating over by bart
in thread Is it safe to append to the array you are iterating over by DrWhy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-08-12 20:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found