How can I verify that a value is present in an array (list) in Perl?

前端 未结 8 951
生来不讨喜
生来不讨喜 2020-12-02 20:47

I have a list of possible values:

@a = qw(foo bar baz);

How do I check in a concise way that a value $val is present or absent

8条回答
  •  醉梦人生
    2020-12-02 21:51

    Interesting solution, especially for repeated searching:

    my %hash;
    map { $hash{$_}++ } @a;
    print $hash{$val};
    

提交回复
热议问题