Re: Searching hash if a given value exists

2007-01-19 Thread Rob Dixon
Michael Alipio wrote: > Hi, > >> - Original Message From: Igor Sutton <[EMAIL PROTECTED]> To: >> Mumia W. <[EMAIL PROTECTED]> Cc: Beginners List >> Sent: Friday, January 19, 2007 4:42:09 PM Subject: Re: >> Searching hash if a given value exist

Re: Searching hash if a given value exists

2007-01-19 Thread Michael Alipio
Hi, > - Original Message > From: Igor Sutton <[EMAIL PROTECTED]> > To: Mumia W. <[EMAIL PROTECTED]> > Cc: Beginners List > Sent: Friday, January 19, 2007 4:42:09 PM > Subject: Re: Searching hash if a given value exists > [...] &g

Re: Searching hash if a given value exists

2007-01-19 Thread Dr.Ruud
"Igor Sutton" schreef: > [attribution repaired] Mumia W: >> 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"; >> } > >

Re: Searching hash if a given value exists

2007-01-19 Thread Igor Sutton
[...] New benchmarks about the subject: foreach_hash_keys: 4 wallclock secs ( 4.40 usr + 0.00 sys = 4.40 CPU) @ 227272.73/s (n=100) foreach_hash_values: 4 wallclock secs ( 3.46 usr + 0.01 sys = 3.47 CPU) @ 288184.44/s (n=100) reverse_hash: 6 wallclock secs ( 6.85 usr + 0.01 sys =

Re: Searching hash if a given value exists

2007-01-19 Thread Igor Sutton
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"; } That's a really good looking idiom, but I see it is less efficient than the fo

Re: Searching hash if a given value exists

2007-01-18 Thread Mumia W.
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

Re: Searching hash if a given value exists

2007-01-18 Thread Rob Dixon
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 (ke

Re: Searching hash if a given value exists

2007-01-18 Thread Jason Roth
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 "i

Re: Searching hash if a given value exists

2007-01-18 Thread Michael Alipio
- Original Message From: Jason Roth <[EMAIL PROTECTED]> To: Michael Alipio <[EMAIL PROTECTED]> Cc: begginers perl.org 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 k

Re: Searching hash if a given value exists

2007-01-18 Thread Jason Roth
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 elemen

Searching hash if a given value exists

2007-01-18 Thread Michael Alipio
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{$_