----- Original Message ----
From: Jason Roth <[EMAIL PROTECTED]>
To: Michael Alipio <[EMAIL PROTECTED]>
Cc: begginers perl.org <[email protected]>
Sent: Friday, January 19, 2007 9:53:49 AM
Subject: Re: Searching hash if a given value exists

Hi Michael,

> To answer your questions, If you want to know if there is already a
> key with a certain value, you have to look through the entire hash.
> However if you want to see if a given key exists, just do "if (exists
> $hash{$key})".  This will take constant time regardless of the number
> of elements in your hash.

I see... so, I really have to do a 

for (keys %hash){
    if ($hash{$_} eq 'somevalue'){
       #do something
    }
}

If I have hundreds of thousands keys and I want to know which contains the same 
values, will I end up waiting too long?




> Hope this helps.

-Jason

On 1/18/07, Michael Alipio <[EMAIL PROTECTED]> 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'!
> }
>
>
> What if I got thousands of keys?
>
> Lastly, how fast is the "if (exists $hash{$key})"? Is it faster than doing a 
> 'for keys' loop, and testing if a given $_ key will appear?
>
>
> Thanks.
>
>
>
>
> ____________________________________________________________________________________
> Never miss an email again!
> Yahoo! Toolbar alerts you the instant new Mail arrives.
> http://tools.search.yahoo.com/toolbar/features/mail/
>







 
____________________________________________________________________________________
Finding fabulous fares is fun.  
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.
http://farechase.yahoo.com/promo-generic-14795097

Reply via email to