I'm working on a browsergame where different shops will require the user to have killed different amounts of different enemies. This is accomplished using two tables inside my database: user_kills, and shop_requirements. I loop through the user's kills to build the %player hash, which looks like this:
%player = { 5 => 2, 4 => 3, }

The keys of the %player hash are enemy ID numbers, and their values are the quantity of that enemy that the player has killed.

In order to figure out which shops a player can access, I retrieve all of them and their requirements, before creating an array of hashes - each hash represents a single store, and is made up just like the %players hash:

%shop = { 2 => 4, 5 => 22, }

What I need to do is loop through this array of hashes, and remove any element in the array where the player's kill counts are less than the amount required by that shop in particular.

I understand that grep is what I'd want to use for this, but I don't know how I would use it to achieve what I want to. This is what I've tried:

my @availablePosts = grep{my $item = $_;foreach my $k (keys %$_) {0 un +less $player{$k} >= $item->{$k}}1;} @posts;

Where @posts is the array of hashes of shop requirements.

If someone could explain this to me, or point me in the right direction, I'd be much obliged.

Spidy


In reply to 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.