in reply to long if statements... What's the best way?

Just one more way to do it: use first from List::util

lhp@nereida:~/Lperl/src/cookbook/ch16$ perl -wde 0 main::(-e:1): 0 DB<1> use List::Util qw(first) DB<2> $var = 'd' DB<3> print "Found $var\n" if (first { $_ eq $var } qw{a e g h d r}) Found d DB<4> $var = 'x' DB<5> print "Found $var\n" if (first { $_ eq $var } qw{a e g h d r}) DB<6>
Cheers

Casiano

Replies are listed 'Best First'.
Re^2: long if statements... What's the best way?
by Herkum (Parson) on Apr 02, 2008 at 19:45 UTC

    any would make more sense in this context. Otherwise the end result is the same.

Re^2: long if statements... What's the best way?
by lodin (Hermit) on Apr 03, 2008 at 12:32 UTC

    Be aware of false strings that match.

    use List::Util 'first'; my $var = '0'; if (first { $_ eq $var } $var) { print "Found.\n"; } else { print "Not found.\n"; } __END__ Not found.
    You could solve this by using defined in this particular case, but it's not a general solution. As Herkum says, any from List::MoreUtils is a better alternative in the general case.

    lodin