in reply to Re: a simple question...
in thread a simple question...

Oh that's what I want. Would @matches have $_ info? or the value of $input{$_} ?


Thanks!

Replies are listed 'Best First'.
Re^3: a simple question...
by wind (Priest) on Nov 28, 2007 at 17:44 UTC
    @matches would contain the keys of the hash that matched. If all you care about are the values, then use this construct:
    if (my @values = grep {$_} @input{qw(info1 info2 info3)}) { print "Matched values: @values"; } else { print "none of the conditions were met\n"; }
    But then again, you could continue to match keys, and use a slice to access the values inside the if. It all depends on what your needs are.
    if (my @matches = grep {$input{$_}} qw(info1 info2 info3)) { print "Matched keys: @matches"; print "Matched values: @input{@matches}"; } else { print "none of the conditions were met\n"; }
    - Miller

      Is there any way to terminate the grep upon a successful match? Not that I think it matters for only three elements, more out of curiosity (and lack of much experience with grep).

        Yes. Use the List::Util first function.
        use List::Util qw(first); if (my $match = first {$input{$_}} qw(info1 info2 info3)) { print "First Matched key: $match"; print "First Matched value: $input{$match}"; } else { print "none of the conditions were met\n"; }
        - Miller
Re^3: a simple question... (map)
by tye (Sage) on Nov 28, 2007 at 17:43 UTC

    grep would give you 'info1', for example. If you want $input{$_}, use map instead.

    - tye