in reply to Re^4: Is there better way to delete dups in an AoH?
in thread Is there better way to delete dups in an AoH?
Here is another way, similar to hv's solution, but generalized so that you don't have to explicitly name the keys. It compares the hashes for equality - ie they must have the same set of keys, and those keys must have the same values. It does this by making a string representation of the hash. Its not so general that it would handle more complex data structures.
#!/usr/bin/perl use Data::Dumper; my $AoH = [ { page => 'spring', chap => 'spring'}, { page => 'spring', chap => 'spring'}, { page => 'winter', chap => 'winter'}, { page => 'spring', chap => 'spring'}, { page => 'spring', chap => 'fall'}, ]; my %dupes; ++$dupes{ stringify( %$_ ) } for @$AoH;; print Dumper \%dupes; @$AoH = grep $dupes{ stringify( %$_ ) } == 1, @$AoH; print Dumper $AoH; sub stringify { my %hash = @_; # explicitly name keys if you want to compare on some #return join '', map { $_, $hash{$_} } ( qw/ page chap / ); # or use them all join '', map { $_, $hash{$_} } sort keys %hash; } __END__ $VAR1 = { 'chapspringpagespring' => 3, 'chapfallpagespring' => 1, 'chapwinterpagewinter' => 1 }; $VAR1 = [ { 'page' => 'winter', 'chap' => 'winter' }, { 'page' => 'spring', 'chap' => 'fall' } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Is there better way to delete dups in an AoH?
by hv (Prior) on Jun 07, 2004 at 01:31 UTC | |
by qq (Hermit) on Jun 08, 2004 at 00:18 UTC |