Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: changing array size in foreach loop: is it safe?

by RazorbladeBidet (Friar)
on Feb 07, 2005 at 18:07 UTC ( [id://428770]=note: print w/replies, xml ) Need Help??


in reply to Re: changing array size in foreach loop: is it safe?
in thread changing array size in foreach loop: is it safe?

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.
Isn't this just saying if one is manipulating an element that is itself an array, not the LIST itself? I would think as long as one stuck to scalars, it should be OK, although one would have to be careful.

Update: This one works... iffy (same ballpark), but works:
@foo = qw/a b c d e f g/; map{ push @foo, 'h' if /^d$/ } @foo; print @foo, "\n";

I think it's a case of simply having enough rope to hang yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://428770]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found