Comparing Multiple Strings In Perl

后端 未结 5 1844
广开言路
广开言路 2021-01-17 23:15

I have my code like this:

if ( $var eq \"str1\" || $var eq \"str2\" || $var eq \"str3\" )
{
...
}

Is there anyways to optimize this. I want

5条回答
  •  误落风尘
    2021-01-17 23:51

    For a list of fixed strings, convert your list to a hash. This is especially useful if you are going to check against your list several times, and if your list gets larger.

    %on_my_list = map {; $_ => 1 } 'str1', 'str2', 'str3', ...;
    
    if ($on_my_list{$var}) { ... }
    

提交回复
热议问题