finding if a hash entry exists

Discussion in 'Perl Misc' started by Zebee Johnstone, Oct 17, 2005.

  1. 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

    --
    Zebee Johnstone (), proud holder of
    aus.motorcycles Poser Permit #1.
    "Motorcycles are like peanuts... who can stop at just one?"
    Zebee Johnstone, Oct 17, 2005
    #1
    1. Advertising

  2. 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/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 17, 2005
    #2
    1. Advertising

  3. 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
    Jürgen Exner, Oct 17, 2005
    #3
  4. 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
    Zebee Johnstone, Oct 17, 2005
    #4
  5. "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.
    Peter Sundstrom, Oct 17, 2005
    #5
  6. Zebee Johnstone

    Dr.Ruud Guest

    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."
    Dr.Ruud, Oct 17, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George
    Replies:
    1
    Views:
    6,194
    Jamie
    Feb 25, 2005
  2. Totan
    Replies:
    0
    Views:
    937
    Totan
    Apr 17, 2006
  3. AtomicBob
    Replies:
    14
    Views:
    871
    Toby Inkster
    May 2, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,141
  5. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
Loading...

Share This Page