Re: How to find if a key exist in hash?

2004-09-29 Thread Gunnar Hjalmarsson
Edward Wijaya wrote:
Subject: How to find if a key exist in hash?
There is a builtin Perl function for the purpose.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



RE: How to find if a key exist in hash?

2004-09-29 Thread NYIMI Jose \(BMB\)


 -Original Message-
 From: Edward Wijaya [mailto:[EMAIL PROTECTED] 
 Sent: Wednesday, September 29, 2004 12:17 PM
 To: [EMAIL PROTECTED]
 Subject: How to find if a key exist in hash?
 
 
 Hi,
 
 I have the following code,
 and I know it is HORRIBLE.
 
 I wonder if I can do it in more
 efficient and elegant way?
 
 Thanks so much
 and
 
 Regards,
 Edward WIJAYA
 SINGAPORE
 
 
 __BEGIN__
 use strict;
 use warnings;
 use Getopt::Std;
 use Data::Dumper;
 
 my %hash = (
A = 'blabla',
B = 'dadada',
C = 'tititi',
 );
 
 my $s = 'A';
 my $get = check_ifHash_key_exist(\%hash, $s);
 print Got it: $get\n;
 
 #--- this is how I do it (don't laugh) --
 
   sub check_ifHash_key_exist {
 
   my ($hash, $str) = @_;
   my $found_str;
   my @array = keys %{$hash};
   for (my $i= 0; $i  keys %{$hash}; $i++) {
 if ($array[$i] eq $str) {
  $found_str = $array[$i];
 last;
 }
  }
   return $found_str;
   }
 __END__


perldoc -f exists

See Also:
http://search.cpan.org/~bpowers/Hash-NoVivify-0.01/NoVivify.pm

HTH,

José.


 DISCLAIMER 

This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer.

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


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




Re: How to find if a key exist in hash?

2004-09-29 Thread max4o

%a = (
   a = 1,
   b = 2,
   c = 3
);

$searchKey = a;

print Found $searchKey if defined($a{$searchKey});

-
This mail is from: [EMAIL PROTECTED]
-


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




Re: How to find if a key exist in hash?

2004-09-29 Thread John W. Krahn
[EMAIL PROTECTED] wrote:
%a = (
   a = 1,
   b = 2,
   c = 3
);
$searchKey = a;
print Found $searchKey if defined($a{$searchKey});
Try that with this hash:
my %a = (
a = undef,
b = undef,
c = undef,
);
defined() does not tell you if a key exists.
perldoc -f exists
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response