in reply to a simple question...

if (my @matches = grep {$input{$_}} qw(info1 info2 info3)) { print "Matched: @matches"; } else { print "none of the conditions were met\n"; }
- Miller

Replies are listed 'Best First'.
Re^2: a simple question...
by Anonymous Monk on Nov 28, 2007 at 17:36 UTC
    Oh that's what I want. Would @matches have $_ info? or the value of $input{$_} ?


    Thanks!
      @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).

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

      - tye