On 01/18/2007 07:46 PM, Michael Alipio wrote:
Hi,

Suppose I have a hash:

my %hash = (dog => 'house', pig => 'barn', bird=> 'cage');

Now I want to know if there is already a key with a 'house' value as I do not 
want to create another key with the same value.
Is there any other faster way to do it than doing a

for (keys %hash){
if ($hash{$_} eq 'house'){
#found 'house'! }

[...]

Yes there is a faster way. Use the "reverse" function:

    my %hash = (dog => 'house', pig => 'barn', bird=> 'cage');
    my %rhash = reverse %hash;
    if ($rhash{house}) {
        print "Found house.\n";
    }


:-)


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to