cyphy, you got homework to do over Christmas break?
Bummer!

Update: Anyway, here's my approach. Note that I do not die in the extraction function on zero members; I feel this sort of thing should be left to the caller. (Note: Older versions of Test::More don't have note.)

>perl -wMstrict -le "my %families = ( flintstones => { dad => 'fred', mom => 'wilma', kid => 'bambam', }, simpsons => { dad => 'fred', mom => 'marge', kid => 'bart', sis => 'lisa', }, ); use Test::More 'no_plan'; my @got; is @got = get_members(\%families => kid => 'bart'), 1, 'got bart'; note qq{families with bart: '@got'}; is @got = get_members(\%families => dad => 'fred'), 2, 'got freds'; note qq{families with freds: '@got'}; is @got = get_members(\%families => mom => 'june'), 0, 'no junes'; note qq{families with june: '@got'}; is @got = get_members(\%families => sis => 'lisa'), 1, 'got lisa'; note qq{families with lisa: '@got'}; is @got = get_members(\%families => xxx => 'yyy'), 0, 'no xxx'; note qq{what's an xxx? '@got'}; sub get_members { my ($hr_families, $member, $name) = @_; return grep $hr_families->{$_}{$member} eq $name, grep exists($hr_families->{$_}{$member}), keys %$hr_families ; } " ok 1 - got bart # families with bart: 'simpsons' ok 2 - got freds # families with freds: 'simpsons flintstones' ok 3 - no junes # families with june: '' ok 4 - got lisa # families with lisa: 'simpsons' ok 5 - no xxx # what's an xxx? '' 1..5

Also: Noticed that undefined family members would generate 'Uninitialized ...' warnings; fixed. (The two grep statements could be combined into one.)


In reply to Re: Finding hashes in hashes of hashes by AnomalousMonk
in thread Finding hashes in hashes of hashes by cyphy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.