This got me thinking - it's one of those timtowtdi thingies, but I thought this solution was quite nice - to begin with - look at this
# First, but @items into a hash instead though...
my @array = qw(dog cat cow horse donkey chicken);
my %items = qw(cat 1 donkey 1);
# this is weird.. if this is true
print "\$array[-4] = $array[-4]\n";
for (1..@array) {
splice(@array, -$_, 1) if $items{ $array[-$_] };
}
# and this is true
print "\$array[-4] = $array[-4]\n";
# how come this is right???
print join ', ',@array, "\n";
And then I thought about it. Hang on. The array's getting spliced each time a match is made, so an index of -$_ would skip elements (ie
$array[-4] before is 'cow', after, it's 'dog', which is expected, but... Weird thing is, it doesn't look like it should work. Anyone got any idea why this works when I think it shouldn't???
err
cLive ;-)
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.