Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

hash key confusion

by Anonymous Monk
on Mar 24, 2014 at 20:28 UTC ( [id://1079591]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

im not having much success when trying to define hash keys to be hexadecimal values. i can demonstrate the problem with this small code fragment...

$hash{"\x61"} = 'a'; print $hash{"\x61"} . "\n"; #prints 'a' print $hash{97} . "\n"; #null output

my naive question (not being a perlmonk) - should not the key value for $hash{"\x61"} be identical to $hash{97} - and therefore the outputs be the same? assuming, as i do that "\x61" equates to decimal 97. i do not understand why both print statements yield different outputs. i would have thought/expected that both print statements should have yielded 'a' as the output.

Replies are listed 'Best First'.
Re: hash key confusion
by davido (Cardinal) on Mar 24, 2014 at 20:44 UTC

    "\x61" interpolates down to the letter "a". The value of 97 stringifies to the string, "97", and no further interpolation is done. Therefore, you're storing a hash key named 'a', and looking for a hash key named '97'.

    The fact that the literal "\x61" interpolates to the character 'a' is discussed in perlop under Quote-like Operators. If you want the literal decimal number 97 to be used as the ASCII (or code-point) value of a character, you should use chr: print $hash{chr(97)}, "\n";


    Dave

Re: hash key confusion
by BrowserUk (Patriarch) on Mar 24, 2014 at 20:46 UTC

    Because "\x61" is the byte character:'a'; whereas 97, is taken as the string '97':

    $h{"\x61"} = 'a';; $h{97} = 'b';; pp \%h;; { 97 => "b", a => "a" }

    What you might want is print $hash{chr(97)};.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: hash key confusion
by Kenosis (Priest) on Mar 24, 2014 at 20:40 UTC

    Try:

    print $hash{chr 97};

    $hash{"\x61"} = 'a'; creates the key a with an associated value of a.

Re: hash key confusion
by sundialsvc4 (Abbot) on Mar 24, 2014 at 20:55 UTC

    My recommendation is that you should convert that hexadecimal-value into an ASCII hexadecimal string, and use that as the hash-key.

    Otherwise, you run the risk that Perl will, well, “try too hard” to interpret your hex-value as a string ... say, as a Unicode string ... and, maybe, as an ill-formed string.   (It’s never a good thing when computers “try too hard” ... heh ... especially when their “perfectly reasonable guesses” are altogether wrong for what you are trying to do.)   Therefore, remove all need to ask the computer to guess.   Just convert the hexadecimal data into a byte-by-byte representation in nice, 1970’s ASCII, and use that as the key.   Suddenly, the entire situation is “unambiguous.”   Perfect.

      Which is better?

      # This? if( exists $hash{ sprintf "%#x", ord('a') } ) { ... } # or this? if( exists $hash{a} ) { ... }

      I'd say that we don't know enough about his needs to make the determination that he ought to be storing stringified hexadecimal values as hash keys. There may be specific cases where that's useful, but in the general case it smells bad to me.

      It almost seems like he's trying to create a hash that crossreferences hex values with literal characters. That's working too hard, when chr, ord, and sprintf already have the conversions down pat.


      Dave

        In making my recommendation, I don’t / didn’t know that there was any sort of “intrinsic meaning” in the keys that the OP intends to store.   If the true target of his intentions is, indeed, characters, versus the hex/binary representations of the same, then obviously a blind binary-to-hex conversion would not be sufficient for his/her purposes.   (In which case, “good catch.”)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1079591]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found