dirtdog has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I'm struggling with getting a unique list printed out within an Array of Hashes. I have a small example that shows baseball players that played for a team. I'm trying to get each player to show only once per team as follows:
However, what I end up getting is:$VAR1 = { 'MARINERS' => [ 'GRIFFEY', 'PEREZ' ], 'REDS' => [ 'GRIFFEY', 'PEREZ', 'ROSE', 'BENCH' ], 'PHILLIES' => [ ROSE' ] };
$VAR1 = { 'MARINERS' => [ 'GRIFFEY', 'PEREZ' ], 'REDS' => [ 'GRIFFEY', 'GRIFFEY', 'PEREZ', 'ROSE', 'BENCH' ], 'PHILLIES' => [ 'ROSE', 'ROSE' ] };
The sample code I have is:
use strict; use constant DEBUG => 2; my $team; my $player; my %teamAccts; my %teamAcctsUniq; my $teamAccts; my $teamAcctsUniq; while (<DATA>) { chomp; if ( (/^T:/) ){ $team = (split /:/) [1]; } if ( (/^P:/) ) { $player = (split /:/) [1]; push( @{$teamAccts{$team}}, $player); } } my %seen; @$teamAcctsUniq = grep { ! $seen{$_->{player}}++ } @$teamAccts; #print Dumper(\%teamAccts); print Dumper(\%teamAcctsUniq); __DATA__ T:REDS P:GRIFFEY P:GRIFFEY P:PEREZ P:ROSE P:BENCH T:PHILLIES P:ROSE P:ROSE T:MARINERS P:GRIFFEY P:PEREZ
any help you could provide would be greatly appreciated. thank you.
2019-10-31 Athanasius added code tags around the data.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unique Values within AOH
by hippo (Archbishop) on Oct 30, 2019 at 14:37 UTC | |
by AnomalousMonk (Archbishop) on Oct 30, 2019 at 18:24 UTC | |
by hippo (Archbishop) on Oct 30, 2019 at 23:56 UTC | |
by AnomalousMonk (Archbishop) on Oct 31, 2019 at 01:40 UTC | |
by dirtdog (Monk) on Oct 30, 2019 at 14:54 UTC | |
|
Re: Unique Values within AOH
by NetWallah (Canon) on Oct 30, 2019 at 16:46 UTC | |
|
Re: Unique Values within AOH
by johngg (Canon) on Oct 30, 2019 at 15:50 UTC | |
by afoken (Chancellor) on Oct 30, 2019 at 20:48 UTC | |
by johngg (Canon) on Oct 30, 2019 at 22:46 UTC | |
|
Re: Unique Values within AOH
by choroba (Cardinal) on Oct 30, 2019 at 18:22 UTC | |
|
Re: Unique Values within AOH
by GrandFather (Saint) on Oct 31, 2019 at 02:18 UTC |