Similar to GrandFather's reponse; this produces the indices (of accessible posts, in posts array).
#!/usr/local/bin/perl use warnings; use strict; use Data::Dumper; my %pangolin = ( 'ant' => 15 , 'termite' => 34 ); my %komodo = ( 'newt' => 34 , 'crock' => 3 , 'ant' => 60 , 'termite' => 90 ); my @shops = ( { 'ant' => 10 , 'termite' => 14 } , { 'newt' => 25 , 'crock' => 6 , 'termite' => 80 } , { 'crock' => 3 } ); for my $killer ( \%pangolin , \%komodo ) { # Prints player's statistics & accessible shops as indices of @shop +s. my @ok = find_accessible_shops( $killer , \@shops ); print Dumper( $killer , \@ok ); } exit; # Given a player's statistics as hash reference & a array reference o +f hash # references of shops, returns the indices (of the array reference). sub find_accessible_shops { my ( $player , $shops ) = @_; my @accessible; DEALER: for my $dealer ( 0 .. scalar @{ $shops } - 1 ) { my $allow; REQUIRE: for my $victim ( keys %{ $shops->[ $dealer ] } ) { next DEALER unless exists $player->{ $victim }; $allow = ( defined $allow ? $allow : 1 ) && $shops->[ $dealer ]->{ $victim } <= $player->{ $victim } ; } push @accessible , $dealer if $allow; } return @accessible; }

About 18.5 hours later: Above sub now returns a list instead of array reference, which was originally there to mistakenly simplify the print Dumper( ... ) statement.

About 2 days later, in the sub, removed explicit sentinel variable (to inidicate first iteration, set to -1) as $allow can be appropriated for the purpose (see my $allow;). That also takes care of the bug in case the inner loop does not run causing @accessible to fill erroneously (note to self: -1 is a true value).


In reply to Re: Using grep for many-to-many relationships by parv
in thread Using grep for many-to-many relationships by Spidy

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.