Aquariophile: if $hash{"foo"} returnt false obwohl es nur undef ist

Hallo!

====================================================
Mein Perl-Script.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my %hash = ( "inge" => "21",
          "tom" => "14",
          "Anna" => "12"
          );

delete $hash{"inge"};
undef $hash{"tom"};

print "The Key <u>inge</u> is NOT True!<br>\n" if $hash{"inge"};
print "The Key <u>tom</u> is NOT True!<br>\n" if $hash{"tom"};
print "The Key <u>Anna</u> is True!<br>\n" if $hash{"Anna"};

====================================================
Die Ausgabe:

The Key Anna is True!

====================================================
Die Frage:

$hash{"inge"} lösche ich ja mit "delete",
dass da     if $hash{"inge"}   false returnt sehe ich ein.

Aber $hash{"tom"} ist doch nur stillgelegt mit "undef"
also sollte der ja grundsaetzlich existieren,
nur eben ohne inhalt, so wie bei  $foo = undef;

Warum der "tom" also Nicht true returnt verstehe ich absolut nicht!!

Das mit $hash{"Anna"} geht wie es sollte.

Danke!
Aquariophile

  1. Hi Aquariophile,
    [....] doch nur stillgelegt mit "undef"

    also sollte der ja grundsaetzlich existieren,
    nur eben ohne inhalt, so wie bei  $foo = undef;

    Ein Element kann nur wahr sein, wenn es definiert ist.....

    Viele Grüße
    Mathias Bigge

  2. Hi!

    Aber $hash{"tom"} ist doch nur stillgelegt mit "undef"
    also sollte der ja grundsaetzlich existieren,
    nur eben ohne inhalt, so wie bei  $foo = undef;

    Warum der "tom" also Nicht true returnt verstehe ich absolut nicht!!

    Weil Du danach gefragt hast, ob der einen wahren Wert hat. Wenn Du nur die Existenz des Keys feststellen willst, musst Du Deine Frage schon dementsprechend formulieren:

    if (exists($hash{"tom"))

    perldoc -f exists

    So long

    --
    Bier trinken fetzt!!!