Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding if a hash entry exists

Reply
Thread Tools

finding if a hash entry exists

 
 
Zebee Johnstone
Guest
Posts: n/a
 
      10-17-2005
THis has got to be documented somewhere, but where? Couldn't find it
in learning perl - where they document what happens, but not how to get
around it, nor in perlreftut or perldsc. It's an obvious problem so
please point me where to look.

I have a hash, $hash.

If may or may not have an entry
$hash->{'fred"}

that entry if it exists, may or may not have the value 0.

How do I determine if it exists without warnings if it doesn't?

#!/usr/bin/perl -w
use strict;
use Data:umper;

my $hash;
$hash->{'one'} = 1;
$hash->{'zero'} = 0;

for my $item (qw(one zero two)) {
if ($hash->{$item}) {
print "$item $hash->{$item}\n";
}
else {
print "$item doesn't exist\n";
}
}
v

gives

one 1
zero doesn't exist
two doesn't exist

$hash->{'zero'} does exist, but how to determine it?

If the if statement is
if (($hash->{$item}) or ($hash->{$item} == 0))
then I get warnings

one 1
zero 0
Use of uninitialized value in numeric eq (==) at z.pl line 10.
Use of uninitialized value in concatenation (.) or string at z.pl line
11.
two

is stopping warnings for just that section the only solution?

Zebee

--
Zebee Johnstone (), proud holder of
aus.motorcycles Poser Permit #1.
"Motorcycles are like peanuts... who can stop at just one?"
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-17-2005
On 2005-10-17, Zebee Johnstone <> wrote:
> THis has got to be documented somewhere, but where? Couldn't find it
> in learning perl - where they document what happens, but not how to get
> around it, nor in perlreftut or perldsc. It's an obvious problem so
> please point me where to look.
>
> I have a hash, $hash.
>
> If may or may not have an entry
> $hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
>
> How do I determine if it exists without warnings if it doesn't?
>


You have asked a SAQ (self answering question). See
http://www.ginini.com/perlsaq.html for more examples of SAQs.

perldoc -f exists

Read also:

perldoc perltoc
perldoc perlfunc
perldoc perlfaq

Yes, all of them, in their entirety.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-17-2005
Zebee Johnstone wrote:
> I have a hash, $hash.
>
> If may or may not have an entry
> $hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
> How do I determine if it exists without warnings if it doesn't?


This is a SAQ (Self Answering Question): perldoc -f exists

jue


 
Reply With Quote
 
Zebee Johnstone
Guest
Posts: n/a
 
      10-17-2005
In comp.lang.perl.misc on Mon, 17 Oct 2005 04:55:08 GMT
Jürgen Exner <> wrote:
> Zebee Johnstone wrote:
>> I have a hash, $hash.
>>
>> If may or may not have an entry
>> $hash->{'fred"}
>>
>> that entry if it exists, may or may not have the value 0.
>> How do I determine if it exists without warnings if it doesn't?

>
> This is a SAQ (Self Answering Question): perldoc -f exists


Thank you,.

Once I realised 'exists' was a function and that wasn't an odd way of
saying "try perldoc -f"

Zebee
 
Reply With Quote
 
Peter Sundstrom
Guest
Posts: n/a
 
      10-17-2005

"Zebee Johnstone" <> wrote in message
news:...
> THis has got to be documented somewhere, but where? Couldn't find it
> in learning perl - where they document what happens, but not how to get
> around it, nor in perlreftut or perldsc. It's an obvious problem so
> please point me where to look.
>
> I have a hash, $hash.
>
> If may or may not have an entry
> $hash->{'fred"}
>
> that entry if it exists, may or may not have the value 0.
>
> How do I determine if it exists without warnings if it doesn't?


Congratulations! You've earned yourself an entry in the Perl SAQ.

http://www.ginini.com/perlsaq.html

There must be something in the water at the moment. The Perl SAQ has had a
recent flurry of activity.



 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-17-2005
Zebee Johnstone schreef:

> I have a hash, $hash.


Isn't that a hash reference?


> If may or may not have an entry
> $hash->{'fred"}


It can't have: the quotes don't match.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
System.ArgumentException: An entry with the same key already exists. George ASP .Net 1 02-25-2005 08:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57