bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monasterians,
I'm trying to refine my Perl skills. I can usually write enough code to make something happen, but without being 'obfuscative' I'd like to write more efficient code.

I'm trying to delete duplicate values in a AoH for later use in HTML::Template. This code does the trick, but maybe a round of golf is needed. I did find this elegant solution in Q&A for singling out duplicate values in a straight hash:
my @keys = grep { $hash{$_} eq $value } keys %hash;
but the AoH structure has me a bit confused how this apply. Anyway, here's my code-that-works-but-looks-too-long. Thanks!
#!/usr/bin/perl use Data::Dumper; use strict; my @AoH = ( { page => 'spring' }, { page => 'winter' }, { page => 'fall' }, { page => 'summer' }, { page => 'spring' } ); my @allvalues; for my $i ( 0 .. $#AoH ) { push ( @allvalues, $AoH[$i]{ page } ); } my %seen; my @duplicates = grep { $seen{$_} ++ } @allvalues; for ( @duplicates ) { for my $i ( 0 .. $#AoH ) { if ( $AoH[$i]{ page } eq $_ ){ delete $AoH[$i]{ page }; splice @AoH,$i,1; last; } } } print Dumper (@AoH);

—Brad
"A little yeast leavens the whole dough."

Replies are listed 'Best First'.
Re: Is there better way to delete dups in an AoH?
by Happy-the-monk (Canon) on Jun 06, 2004 at 13:50 UTC

    my %uniq = map { $_->{page} => 1 } @AoH; @AoH = map { { page => $_ } } keys %uniq;

    This one would destroy all data that is not a value of the "page" key. But it solves the particular problem.

      Happy-the-monk, that was stunning! I love it when I can replace 15 lines of code with only 2. I plugged it in and worked like a charm.

      To be honest, I'm not sure how it works. My educated guess is it iterates through @AoH and assigns a key/value pair of page => 1 for every element. But why does it skip the 5th element (the dupe)? When I Dumper, I get:
      $VAR1 = 'fall'; $VAR2 = 1; $VAR3 = 'winter'; $VAR4 = 1; $VAR5 = 'spring'; $VAR6 = 1; $VAR7 = 'summer'; $VAR8 = 1;
      So, 1) why/how does it skip the dupe? 2) what does assigning the 1 do? 3) what does that 2nd line do to delete the key/value pairs of page => 1? Also, any good sources for learning how to disect a map/grep would be much appreciated. Thanks again.

      Update: Found this helpful resource in the meantime.

      —Brad
      "A little yeast leavens the whole dough."

        1) why/how does it skip the dupe?

        It actually doesn't skip it. It's just that it is assigned as a key to a hash, and hash keys are unique, thus it overwrites the old key without doing harm.

        2) what does assigning the 1 do?

        It's just a random value, you could assign anything you like as a value to the hash. It's not important, as long as there's something there to make perl happy.

        3) what does that 2nd line do to delete the key/value pairs of page => 1?

        nothing. It now restores the AoH. Hmm, I didn't tidy up the "%uniq" hash if you thought of that.

        Also, any good sources for learning how to disect a map/grep

        Besides   perldoc -f grep   and perldoc -f map? Hmm. Reading Perlmonks... ;)

        Cheers, Sören

Re: Is there better way to delete dups in an AoH?
by hv (Prior) on Jun 06, 2004 at 14:31 UTC

    my @allvalues; for my $i ( 0 .. $#AoH ) { push ( @allvalues, $AoH[$i]{ page } ); }

    In general, building up an array by pushing in a loop can usually be done more elegantly with map/grep:

    my @allvalues = map $_->{page}, @AoH;

    However, if the only purpose for this array is to locate the duplicates for deletion, you don't really need this array or the duplicates array:

    # count occurrences of each value my %count; ++$count{$_->{page}} for @AoH; # keep only those copies that aren't duplicated @AoH = grep $count{$_->{page}} == 1, @AoH;

    That is:

    # replace @AoH with @AoH = # only grep # the hashrefs whose 'page' value appears exactly once $count{$_->{page}} == 1, # of the current @AoH @AoH;

    Hugo