Looks like you have been well taken care of regarding your inquiry, but I would like to add a cool use of
each I recently discovered. Should fit well with your code assignment. You can use
each to collect the indices of an array, or both indices and values. From the docs:
each HASH
each ARRAY
When called on a hash in list context, returns a 2-element list
consisting of the key and value for the next element of a hash. In
Perl 5.12 and later only, it will also return the index and value
for the next element of an array so that you can iterate over it;
older Perls consider this a syntax error. When called in scalar
context, returns only the key (not the value) in a hash, or the
index in an array.
So, using each, your code can go from:
$ perl -e '
my @colors = qw(red green blue yellow pink purple brown);
my $count = @colors;
my @drop = qw(pink brown);
my $num = 0;
foreach $num (1..$count){
$num--;
if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){
splice (@colors, $num, 1);
}
}
print "@colors \n";
'
to:
$ perl -e '
my @colors = qw(red green blue yellow pink purple brown);
my @drop = qw(pink brown);
while ( my ($num, $val) = each @colors ) {
if ($val eq $drop[0] or $val eq $drop[1]) {
splice (@colors, $num, 1);
}
}
print "@colors \n";
'
both produce the following output:
__output__
red green blue yellow purple
I hope that was the correct answer! :)