in reply to Eliminate exact duplicates from array of hashes

I really like tybalt89's approach here: Re: Eliminate exact duplicates from array of hashes.

Unfortunately it requires an eval step...

Here a way to avoid it

use strict; use warnings; use Data::Dump qw/pp dd/; my @test_data = ( { Tag1 => "1", Tag2 => "a" }, { Tag1 => "1", Tag2 => "a" }, { Tag1 => "1", Tag2 => "b" }, { Tag1 => "1", Tag2 => "c" }, { Tag1 => "1", Tag2 => "a" }, { Tag1 => "2", Tag2 => "a" }, { Tag1 => "2", Tag2 => "d" }, { Tag1 => "2", Tag2 => "a" }, { Tag1 => "3"}, { Tag1 => "sun", Tag2 => "a" }, { Tag1 => "sun", Tag2 => "a" }, ); my %seen; my @unique = grep { not $seen{pp $_}++ } @test_data; #pp \%seen; pp \@unique;

Sadly uniq doesn't offer to provide an optional block (analog to sort ) to emulate this behavior with code like

uniq { pp $_ } @test_data

Please be aware of possible side effects when having circular data.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Eliminate exact duplicates from array of hashes
by LanX (Saint) on Oct 10, 2019 at 02:28 UTC
    Here a version with Data::Dumper which is core. (though Data::Dump is always my first installation)

    use strict; use warnings; use Data::Dumper; use Test::More; sub uniq_nds{ # uniqe nested data-structures my %seen; local $Data::Dumper::Sortkeys = 1; local $Data::Dumper::Terse = 1; grep { not $seen{Dumper $_}++ } @_; } my @test_data = ( { Tag1 => 1, Tag2 => "a" }, { Tag1 => 1, Tag2 => "a" }, { Tag1 => 1, Tag2 => "b" }, { Tag1 => 1, Tag2 => "c" }, { Tag1 => 1, Tag2 => "a" }, { Tag1 => 2, Tag2 => "a" }, { Tag1 => 2, Tag2 => "d" }, { Tag1 => 2, Tag2 => "a" }, { Tag1 => 3 }, { Tag1 => "sun", Tag2 => "a" }, { Tag1 => "sun", Tag2 => "a" }, ); my $got = [ uniq_nds @test_data ]; my $expected = [ { Tag1 => 1, Tag2 => "a" }, { Tag1 => 1, Tag2 => "b" }, { Tag1 => 1, Tag2 => "c" }, { Tag1 => 2, Tag2 => "a" }, { Tag1 => 2, Tag2 => "d" }, { Tag1 => 3 }, { Tag1 => "sun", Tag2 => "a" }, ]; is_deeply( $got, $expected, 'uniq AoH' ) or diag Dumper $got; done_testing;

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice