in reply to check if a value is in a given set

I will probably write this using List::Util like so:

#!/usr/bin/perl use warnings; use strict; use List::Util qw(first); my $var = 'apple'; my $h_ref = { banana => 1, plum => 1, apple => 1, strawberry => 1, pea => 1, }; my $foo = first { $_ eq $var } keys %{$h_ref}; defined $foo ? print "yes" : print "No";
If your $var exist, it prints "Yes", if not you get a "No"

Replies are listed 'Best First'.
Re^2: check if a value is in a given set
by Anonymous Monk on Jul 24, 2012 at 17:45 UTC

    You have a set of hash keys, and enumerate through them to find whether the one you seek exists?