Mia Lua
Mia Lua

Reputation: 155

How to look for a value in a hash?

I have a hash like

%has  = ('TCA' =>'S', 'TTC'=>'N'....)

and a

$string = 'TCA' 

I want to look for my $string in the %has and if it exist, print the value using perl like this:

TCA, S

How can I do that? Thank you so much!

Upvotes: 1

Views: 74

Answers (1)

TLP
TLP

Reputation: 67920

Check if the value exists.

if (exists $has{$string}) {
     printf "%s, %s\n", $string, $has{$string};
}

Keep in mind that this is case sensitive.

You should probably read up on the various Perl functions in perldoc perlfunc

Upvotes: 3

Related Questions