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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.