How to test if a value exist in a hash?

后端 未结 6 1348
-上瘾入骨i
-上瘾入骨i 2020-12-08 12:20

Let\'s say I have this

#!/usr/bin/perl

%x = (\'a\' => 1, \'b\' => 2, \'c\' => 3);

and I would like to know if the value 2 is a ha

6条回答
  •  醉话见心
    2020-12-08 12:45

    grep and values

    my %x = ('a' => 1, 'b' => 2, 'c' => 3); 
    
    if (grep { $_ == 2 } values %x ) {
        print "2 is in hash\n";
    }
    else {
        print "2 is not in hash\n";
    }
    

    See also: perldoc -q hash

提交回复
热议问题