Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Find duplicate values in hash

by whakka (Hermit)
on Apr 10, 2009 at 21:25 UTC ( [id://756921]=note: print w/replies, xml ) Need Help??


in reply to Find duplicate values in hash

for my $key (keys %hash) { my $value_key = "@{[values %{$hash{$key}}]}"; }
gives you an error because the %{...} in %{$hash{$key}} is attempting to treat the value of $hash{$key} as a hash ref - but it's really a scalar, as you say you want. So replace %{$hash{$key}} with %hash to make it work. I see the other post had a double-nested hash, which is why you would need this particular syntax.

It also stores "@{[...]}" in a scalar used as a hash slice - normally you wouldn't need this and could just say values %hash. Hope this helps at all...

Replies are listed 'Best First'.
Re^2: Find duplicate values in hash
by moff (Novice) on Apr 11, 2009 at 13:25 UTC
    Thank you all very much for the quick responses, all of them really helpful and instructive. So far I have tried JavaFan's and Nkuvu's solutions, and they work just fine. (I will need some time to understand exactly how they work though, might come back here if I fail to do so...). Thanks also for the pointers to Data::Dumper and the explanation as to why I was getting that error.
      Hello again, I have finally used JavaFan's code, changing it slightly to get an output similar to Nkuvu's, and managed to package it with PAR to get a standalone program. I can say it works flawlessly even with very large files with many thousands of lines. Thanks again. However, I still do not understand it thoroughly. I think I understand everything except
      push @{$reverse{$value}}, $key;
      What exactly happens here for each pair of keys and values of my first hash? I can see that, after that, in those cases where the value is the same for two or more keys, those two or more keys are contained in one "key array" (?) (one line if I print it out), and subsequently split with local $". But I do not really understand the above line of code. If JavaFan or someone else could explain it with words I would very much appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://756921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found