finding if a hash entry exists

Z

Zebee Johnstone

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::Dumper;

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
 
A

A. Sinan Unur

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
 
J

Jürgen Exner

Zebee said:
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
 
Z

Zebee Johnstone

In comp.lang.perl.misc on Mon, 17 Oct 2005 04:55:08 GMT
Jürgen Exner said:
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
 
P

Peter Sundstrom

Zebee Johnstone said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top