Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Just as there are different operators for comparing strings and numbers, you should be aware how that affects accessing elements of hashes.

Consider the following:

my $key1 = '00001'; my $key2 = 1; my %hash1 = (1 => 1); my %hash2 = (1 , 2); my %hash3; $hash3{$key1} = 3; my %hash4; $hash4{$key2} = 4;
Obviously, $key1 == $key2, but $key1 ne $key2. That is, they are equal as numbers, but since $key1 is a string and $key2 is not, then they will not match using the eq string operator. This leads to the following:

00001 == 1 That is: $key1 == $key2 00001 ne 1 That is: $key1 ne $key2 $hash1{$key1} is undef $hash1{$key2} == 1 $hash2{$key1} is undef $hash2{$key2} == 2 $hash3{$key1} == 3 $hash3{$key2} is undef $hash4{$key1} is undef $hash4{$key2} == 4
This is particularly important when working with databases, for example, where the key field may be zerofilled.

In reply to Hash Keys (strings or numbers?) by robot_tourist

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (9)
    As of 2014-12-27 07:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (176 votes), past polls