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.
In reply to Unique Values within AOH by dirtdog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |