Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
There's more than one way to do things
 
PerlMonks  

Re: How to get Keys from hash having same value?

by Punitha (Priest)
on Jan 07, 2008 at 06:10 UTC ( #660764=note: print w/ replies, xml ) Need Help??


in reply to How to get Keys from hash having same value?

Hi isha,

If you want to manipulate the simple hash, then your hash will be like this,

my %myhash = ( 1 => 'a', 2=> 'b', 3=> 'a', 4=>'c');

To manipulate this simple hash, you can do like

use strict; my %myhash = ( 1 => 'a', 2=> 'b', 3=> 'a', 4=>'c'); for my $key (keys %myhash){ if($myhash{$key} eq 'a'){ print "KEY:$key\n"; } }

But you created the hash as

my %myhash = { 1 => 'a', 2=> 'b', 3=> 'a', 4=>'c'};

which is anonymous hash reference. To manipulate this hash reference, then you can do like

use strict; my $myhash = { 1 => 'a', 2=> 'b', 3=> 'a', 4=>'c'}; for my $key (keys %{$myhash}){ if(${$myhash}{$key} eq 'a'){ print "KEY:$key\n"; } }

Punitha


Comment on Re: How to get Keys from hash having same value?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-04-18 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (468 votes), past polls