Spidy has asked for the wisdom of the Perl Monks concerning the following question:
%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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using grep for many-to-many relationships
by GrandFather (Saint) on Jun 30, 2007 at 03:20 UTC | |
Re: Using grep for many-to-many relationships
by parv (Parson) on Jun 30, 2007 at 04:14 UTC | |
Re: Using grep for many-to-many relationships
by aquarium (Curate) on Jul 02, 2007 at 03:05 UTC | |
Re: Using grep for many-to-many relationships
by Moron (Curate) on Jul 02, 2007 at 11:49 UTC |