in reply to Re: nonempty false list
in thread nonempty false list

Hmm thanks to everyone, I definitely see the distinction. What I am TRYING to do is this: I have a hash, and I want to test whether any of three separate keys produce a 'true' value. So, it looks something like this (I boiled it down for the first example)
if(@opts{qw|a b c|}) { }
Which seemed pretty elegant, but as previously discussed does not work, because its a hash slice. THIS, however, seems horribly inelegant:
if(@{[@opts{qw|a b c|}]}) { }
But perl must have something better than this:
if($opts{a} || $opts{b} || $opts{c}) { }

Replies are listed 'Best First'.
Re^3: nonempty false list
by kennethk (Abbot) on Mar 10, 2011 at 18:52 UTC
    Testing each value of a list sounds like a job for grep. When I need to determine if any elements of a list are true, I will usually use something that looks like:

    if( grep $_, @opts{qw|a b c|}) { }

    A cleaner version of this would be

    if( grep $opts{$_}, qw|a b c|) { }

    You can use a couple negations to check if all values are true:

    if( !grep !$opts{$_}, qw|a b c|) { }

    Note that your second suggestion will always return true, since you will always have a 3-element array in scalar context.

Re^3: nonempty false list
by wind (Priest) on Mar 10, 2011 at 18:59 UTC
    Just use grep
    if (grep {$opt{$_}} qw(a b c)}) {
    And you can even capture the keys for which true is returned
    if (my @keys = grep {$opt{$_}} qw(a b c)}) { print "@keys match\n";
Re^3: nonempty false list
by ikegami (Patriarch) on Mar 10, 2011 at 19:06 UTC