Hello all,

This one has been giving me some grief for awhile now, and it usually isn't an issue, but every now and then it comes up and could use some help in grasping what is going on and a possible change to my approach. (have looked online but can't find a definitive answer)

This occurs whether strict is called or not, and it also happens whether "my" is declared on the hash name or not, so keeping the samples simple and omitting that part...


Here's the test code:


$hashname{foo}='a string';

if($hashname{foo} == 0){
  print "It is zero\n";
}else{
  print "It is NOT zero\n";
}


Run it, and you get:
It is zero

In reading online, it appears that hash values may be treated as strings by default, rather than pure numeric. As a huge amount of my code uses hashes to do both "eq" type tests in addition to numeric "==" tests on a hash value that is stored as a number, it'd help to get some insight on why this isn't working so that I don't introduce bugs in the code. In the sample above, I need it to be sensing the fact that the value stored IS, in fact, a numeric zero. This test result almost appears as though the test itself is setting it to zero, but this can't be the case. It isn't undef, because "a string" has been stored in the hash value memory space. It can't be a numeric zero, because it hasn't been set as such, and it can't be a null value for the same reason that there is a string stored there. Just not sure what is running under the hood that is producing this result. This issue ONLY happens when testing for a numeric 0.


So... one additional test on 0 but with a null value being set:


$hashname{foo}="";
if($hashname{foo} == 0){
  print "It is zero\n";
}else{
  print "It is NOT zero\n";
}


Run it, and you get:
It is zero


I could potentially see this one returning as true/correct for the test, since it could be looking at it as a null value being tested but I'm not super familiar with how Perl handles this in the background. It still messes up my logic handlers, though, because if I need to test for an actual 0 being stored rather than a null value, I'm out of luck.


Along this same line of thought but getting away from the "0" or string values, here's another test:


$hashname{foo}=3;

if($hashname{foo} == 3){
  print "First test: It is 3\n";
}else{
  print "First test: It is NOT 3\n";
}
if($hashname{foo} == 4){
  print "Second test: It is 4\n";
}else{
  print "Second test: It is NOT 4\n";
}


Run it, and you get:

First test: It is 3
Second test: It is NOT 4

So, in the third/final test, it is correctly seeing it as a numeric "3", because it can differentiate between the two and shows that it is not a "4".


If anyone can enlighten me with a deeper understanding of what Perl is doing here, I'd sure appreciate it. At the moment, I can't rely on the code correctly returning a true numeric test for 0 being stored in the hash value memory space... would be great to avoid future WTF moments when building logic routines that actually need that test type to work!

Thanks in advance!

In reply to Hash value test of zero by themcp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.