I have my code like this:
if ( $var eq "str1" || $var eq "str2" || $var eq "str3" )
{
...
}
Is there anyways to optimize this. I want something like:
if ( $var eq ["str1" || "str2" || "str3"] ) {...}
Depending on the contents of the strings, a regex is quite convenient:
if ($var =~ /^(str1|str2|str3)$/) { … }
Failing that, you can grep over a list:
if (grep { $var eq $_ } qw{str1 str2 str3}) { … }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With