in reply to The error says the value is uninitialized, but it works anyway
As a general rule, you should avoid changing an array you are iterating over. That's bound to cause all kinds of headaches. Also, it is favourable to name variables by what they mean, not by what they contain. Here, $num is a bad name. $index is better, so are $i, $j, $k, etc.
Here's my stab at the problem.
holli
You can lead your users to water, but alas, you cannot drown them.
Here's my stab at the problem.
Besides being clearer, this also works with n elements in whatever array, whereas your solution hardcodes two elements in the @drop.use strict; use warnings; my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink brown); foreach my $drop_color (@drop) { my $index = 0; foreach my $color (@colors) { if ( $color eq $drop_color ) { splice (@colors, $index, 1); last; } $index++; } } print "@colors \n";
holli
You can lead your users to water, but alas, you cannot drown them.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: The error says the value is uninitialized, but it works anyway
by jcb (Parson) on Aug 19, 2019 at 00:42 UTC | |
by AnomalousMonk (Archbishop) on Aug 19, 2019 at 17:27 UTC | |
by soonix (Canon) on Aug 19, 2019 at 08:36 UTC |
In Section
Seekers of Perl Wisdom