Searching an example for a defined hash value of a nonexisting hash key

Discussion in 'Perl Misc' started by Ralf Baerwaldt, Jul 20, 2004.

  1. In "man perlfunc" I found:

    -----
    defined EXPR
    ....
    When used on a hash element, it tells you whether
    the value is defined, not whether the key exists
    in the hash.
    -----

    Is it really possible to have a defined value on a
    nonexisting key ? Can someone give a sample ?

    If I want to print the defined values of a hash, do I have to prove
    if the keys exists ? Up to now I just prove for a defined value
    assuming that in this case the key must exists, i.e.

    ----
    #!/usr/bin/perl
    my %h;
    ....
    print "Val=$h{'KEY'}\n" if defined($h{'KEY'});
    ----

    or must I do:

    ----
    print "Val=$h{'KEY'}\n" if (exists($h{'KEY'}) and (defined($h{'KEY'}));
    ----
    Ralf Baerwaldt, Jul 20, 2004
    #1
    1. Advertising

  2. Ralf Baerwaldt

    Paul Lalli Guest

    Re: Searching an example for a defined hash value of a nonexistinghash key

    On Tue, 20 Jul 2004, Ralf Baerwaldt wrote:

    > In "man perlfunc" I found:
    >
    > -----
    > defined EXPR
    > ...
    > When used on a hash element, it tells you whether
    > the value is defined, not whether the key exists
    > in the hash.
    > -----
    >
    > Is it really possible to have a defined value on a
    > nonexisting key ? Can someone give a sample ?


    No. The point of that manual text is that a key/value pair can exist
    without the value being defined.

    As relates to Perl hashes, boolean truth implies defined, and defined
    implies exists.

    > If I want to print the defined values of a hash, do I have to prove
    > if the keys exists ?


    No. If the value is defined, it exists.

    This is a logical series of steps:
    if (exists ($hash{key})) {
    print "key exists in hash\n";
    if (defined ($hash{key})){
    print "key's value is defined in hash\n";
    if ($hash{key}){
    print "key's value is a true value\n";
    } else {
    print "key's value is false\n";
    }
    } else {
    print "key's value is not defined, and therefore false\n";
    }
    } else {
    print "key does not exist. Value is therefore undefined and false\n";
    }


    HTH,
    Paul Lalli
    Paul Lalli, Jul 20, 2004
    #2
    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. Oodini
    Replies:
    1
    Views:
    1,750
    Keith Thompson
    Sep 27, 2005
  2. M P
    Replies:
    1
    Views:
    454
  3. Une bévue
    Replies:
    5
    Views:
    141
    Une bévue
    Aug 10, 2006
  4. Antonio Quinonez
    Replies:
    2
    Views:
    154
    Antonio Quinonez
    Aug 14, 2003
  5. IDK
    Replies:
    11
    Views:
    369
    gautam chekuri
    Jun 29, 2007
Loading...

Share This Page