I checked the
errata pages at O'Reilly but saw no mention of what I think might be an error on page 224 of "Programming Perl, 3rd Edition". More specifically, I'm talking about the following code:
@common = inter ( \%foo, \%bar, \%joe );
sub inter {
my %seen;
for my $href (@_) {
while (my $k = each %$href ) {
$seen{$k}++;
}
}
return grep { $seen{$_} == @_ } keys %seen;
}
=======================================================
When I tried printing
@common using the following data:
__DATA__
my %foo = (
'one' => 1,
'two' => 2,
'three' => 3,
);
my %bar = (
'two' => 5,
'four' => 6,
);
my %joe = (
'three' => 7,
'four' => 8,
'five' => 89,
);
@common came back null for me. However, when I changed this line:
return grep { $seen{$_} == @ } keys %seen;
to this:
return grep { $seen{$_} } keys %seen;
@common returned:
three five one two four
which judging by the description in the book, appears to be what the code is
supposed to yield. I find it hard to believe I would catch an error like this. Can someone please back me up on this?
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot";
$nysus = $PM . $MCF;
Edit by tye
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.