Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Hash element that won't print. Perl Bug???

by monkfish (Pilgrim)
on Oct 10, 2001 at 09:01 UTC ( #117928=perlquestion: print w/replies, xml ) Need Help??

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

I am ready to tear my hair out. I have been debugging for 5 hours now and I have found the bug in my morass of code, but I don't understand why it is occuring...

Here is a sample script that demonstrates the bug.

My element 00 in $hash doesn't print when I know it is there. I want to believe this is a perl bug, but I suspect it is really my not understanding how 00 is getting evaluated or something.

@foo = (0,1); $hash{00} = "data"; $hash{10} = "moredata"; print '$hash{00} is ', $hash{00} , "\n"; print '$hash{$foo[0] . "0"} is ', $hash{$foo[0] . "0"}, "\n"; print '$hash{$foo[1] . "0"} is ', $hash{$foo[1] . "0"}, "\n"; print '$hash{10} is ', $hash{10} , "\n";

The results are:

NOTE LINE 2 after is there is nothing!

$hash{00} is data $hash{$foo[0] . "0"} is $hash{$foo[1] . "0"} is moredata $hash{10} is moredata

What is going on?

-monkfish
The fishy monk.

Replies are listed 'Best First'.
Re: Hash element that won't print. Perl Bug???
by snowcrash (Friar) on Oct 10, 2001 at 09:12 UTC
    $hash{00} is actuallay the same as $hash{0} because the key is a number and so is not interpreted as a string. if you would use warnings or the -w switch you would have spotted this one easier.
    btw: i recently found out that $hash{1_000} is the same as $hash{1000} cos perl allows to use the underscore for legibility in larger numbers like 123_324_234.
    hth
    snowcrash
Re: Hash element that won't print. Perl Bug???
by nardo (Friar) on Oct 10, 2001 at 09:12 UTC
    The answer is discovered with
    print join(', ', keys %hash), "\n";
    which reveals that 00 becomes 0, if you use
    $hash{'00'} = 'data';
    it will work like you want it to.
Re: Hash element that won't print. Perl Bug???
by mirod (Canon) on Oct 10, 2001 at 09:14 UTC

    Using formatted numbers has hash keys is not a good idea. The data is not in $hash{00} but in $hash{0}, as 00 is interpreted as a number and not as a string.

    When in doubdt about your data try either the perl debugger (run perl -d) or Data::Dumper (or Data::Denter if you have it installed):

    #!/bin/perl -w use strict; use Data::Dumper; my @foo = (0,1); my %hash; $hash{00} = "data"; $hash{"00"} = "the real one"; $hash{10} = "moredata"; print Dumper( %hash); print '$hash{0} is ', $hash{0} , "\n"; print '$hash{00} is ', $hash{00} , "\n"; print '$hash{$foo[0] . "0"} is ', $hash{$foo[0] . "0"}, "\n"; print '$hash{$foo[1] . "0"} is ', $hash{$foo[1] . "0"}, "\n"; print '$hash{10} is ', $hash{10} , "\n";
Re: Hash element that won't print. Perl Bug???
by kjherron (Pilgrim) on Oct 10, 2001 at 09:14 UTC
    The 00 in the term $hash{00} is initially evaluated as a number, rather than a string, because it's not in quotes. Perl then converts the number back into a string to store into the hash. As a result, the assignment
    $hash{00} = "data";
    actually stores into $hash{"0"}, not $hash{"00"}.

    Later, when you try to access $hash{0 . "0"}, you do access $hash{"00"}, which of course is not the same as $hash{"0"}.

Re: Hash element that won't print. Perl Bug???
by cLive ;-) (Prior) on Oct 10, 2001 at 09:29 UTC
    In your hash definition 00 is interpolated numerically to become 0. What you need is:
    $hash{'00'} = "data"; $hash{'10'} = "moredata";
    It's just "coincidence" that 10 = "10".

    When you access the hash later, the key is interpolated in string context.

    If you were doing 3 digits, things would be worse, because 010 = "8" (octal) and not "10".

    Make sense?

    cLive ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2022-06-26 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?