Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: The error says the value is uninitialized, but it works anyway

by AnomalousMonk (Archbishop)
on Aug 18, 2019 at 06:34 UTC ( [id://11104633]=note: print w/replies, xml ) Need Help??


in reply to Re: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

Unfortunately, it's still altering an array (or it might have been a hash) over which you're iterating and so still incorrect:

c:\@Work\Perl\monks>perl -wMstrict -le "my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink purple); while ( my ($num, $val) = each @colors ) { if ($val eq $drop[0] or $val eq $drop[1]) { splice (@colors, $num, 1); } } print qq{@colors}; " red green blue yellow purple brown

Update: each sez:

If you add or delete a hash's elements while iterating over it, the effect on the iterator is unspecified; for example, entries may be skipped or duplicated--so don't do that.
The same obviously applies to arrays. The each doc goes on to discuss a specific exception involving delete and hashes, but still no joy WRT arrays.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: The error says the value is uninitialized, but it works anyway
by dbuckhal (Chaplain) on Aug 18, 2019 at 13:23 UTC
    Ahh, of course... thanks!

    adding "save" array, and "if" to "unless"...

    perl -wMstrict -e ' my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink purple); my @save = (); while ( my ($num, $val) = each @colors ) { unless ($val eq $drop[0] or $val eq $drop[1]) { push @save, $colors[$num]; } } print "@save \n"; ' __output__ red green blue yellow brown

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found