Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Removing duplicate values for a hash of arrays

by Eily (Parson)
on Nov 21, 2013 at 18:27 UTC ( #1063772=note: print w/replies, xml ) Need Help??


in reply to Removing duplicate values for a hash of arrays

With a regular hash you just have to do:

%reverseHash = reverse %hash; %hash = reverse %reverseHash;
to eliminate duplicate values. Which of course doesn't work in your case, because the arrays in @hash{'a', 'c'} are no the same, though they contain the same values, and so have different references. Well, you could always turn those arrays into something identical. If you have someFunc(PARAM) that returns the same thing if and only if the values in the data structure PARAM are identical, you can do:
my %reverseHash; while(($key, $value)=each %hash) { $processedValue = someFunc($value); $reverseHash{$processedValue} = $key; } my @keys = values %reverseHash; my %newHash; @newHash{@keys} = @hash{@keys};

The thing is, there is an easy solution for someFunc : Data::Dumper (and this time I'll use map)

use Data::Dumper; my %hash = ( a => [1, 2], b => [2, 3], c => [1, 2] ); my %reverseHash = map { Dumper($hash{$_}) => $_ } keys %hash; %hash = map { $_ => $hash{$_} } values %reverseHash; print Dumper \%hash;

Edit: this works well for nested arrays, even blessed ones. But with hashes, you probably have to activate Data::Dumper's key sorting (It depends of the version of Perl you are using).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063772]
help
Chatterbox?
[Corion]: Yay! As I had to reset my phone again, I now also back up my SMS automatically. Highly conveniently, there is a program to back up my SMS to my IMAP server as mails ;)
[1nickt]: Corion this is an android phone? If so, are you encountering problems with recent upgrades? Lately my phone dies suddenly quite often, seemingly coredumps caused by interaction of two actions, eg phone playing a sound and user swiping simultaneously :-/
[Corion]: 1nickt: Yeah, it's Android, but it's CM 11, so fairly old. I'm looking for a newer (china) phone, but I think I'll wait until Oreo ROMs get out for them
[Corion]: Otherwise, the Vernee Mars looks tempting
[Corion]: ... but alas, no Oreo support in sight yet, and I prefer to flash a stock Android over the custom ROMs, and Oreo promises to make the userland more maintainable.
[LanX]: Corion: shouldn't pmdev have permission to view code of nodelets?
[1nickt]: And of course everything is "unlocked" in EU, right?
[1nickt]: I think there are far more choices than in the US
LanX You don't have access to  Function Nodelet.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-11-18 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?