in reply to 'group by query' from array of hashes

Here's my riff on wind's illustration:

#!perl

use strict;
use warnings;
use utf8;
use open qw( :utf8 :std );

my @players = qw( Bob Carol Ted Alice );
my @deals;   # Array of hashes of cards (values) dealt to players (keys)
my %hand_of; # Hash of arrays of cards in players' (keys) hands

push @deals, { Bob   => '7♦' };
push @deals, { Carol => 'K♣' };
push @deals, { Ted   => '2♥' };
push @deals, { Alice => '4♥' };

push @deals, { Bob   => 'A♣' };
push @deals, { Carol => 'Q♣' };
push @deals, { Ted   => '3♦' };
push @deals, { Alice => 'J♠' };

push @deals, { Bob   => '5♠' };
push @deals, { Carol => 'A♠' };
push @deals, { Ted   => '9♣' };
push @deals, { Alice => 'K♦' };

push @deals, { Bob   => '3♠' };
push @deals, { Carol => 'Q♠' };
push @deals, { Ted   => 'J♦' };
push @deals, { Alice => '6♦' };

push @deals, { Bob   => 'Q♦' };
push @deals, { Carol => 'Q♥' };
push @deals, { Ted   => '7♠' };
push @deals, { Alice => '5♦' };

for my $deal (@deals) {
    my ($player, $card) = %$deal;
    push @{ $hand_of{$player} }, $card;
}

for my $player (@players) {
    my $hand = join ' ', @{ $hand_of{$player} };
    print "$player has $hand\n";
}

__END__
Bob has 7♦ A♣ 5♠ 3♠ Q♦
Carol has K♣ Q♣ A♠ Q♠ Q♥
Ted has 2♥ 3♦ 9♣ J♦ 7♠
Alice has 4♥ J♠ K♦ 6♦ 5♦

  • Comment on Re: 'group by query' from array of hashes