From what I can tell, if you pull the carpet out from underneath Perl, you're going to pay the price. One way to get things on the same wavelength is to either use
grep to do your list filtering:
@list = grep { $_ != 2 && $_ != 4 } @list;
Or, you can go all Old School and do it this way:
my @foo = 1..10;
for (my $i = 0; $i < @foo; $i++)
{
print "$i ($foo[$i])\n";
if ($foo[$i] == 5 || $foo[$i] == 6)
{
splice(@foo, $i, 1);
redo;
}
}
The
redo is important because it prevents the for loop from incrementing $i and thereby skipping an entry. This way, you're keeping pretty "close to the metal" and nothing will slip by.
Update:
As
merlyn has suggested, this so-called "Old School" code is not an example of how it should be done, and is in fact, an example to the contrary. Using
map and
grep is going to be more effective and less prone to programmer error virtually every time, so that's what I do. With the example, I was merely trying to show how difficult it was to do if you chose not to use grep.