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:

$VAR1 = { 'MARINERS' => [ 'GRIFFEY', 'PEREZ' ], 'REDS' => [ 'GRIFFEY', 'PEREZ', 'ROSE', 'BENCH' ], 'PHILLIES' => [ ROSE' ] };
However, what I end up getting is:
$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

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.