Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Hash references and illegal octal digits

by peppiv (Curate)
on Dec 24, 2002 at 15:48 UTC ( #222103=perlquestion: print w/ replies, xml ) Need Help??
peppiv has asked for the wisdom of the Perl Monks concerning the following question:

Yesterday I posted a question about getting data from an array and I was given an excellent answer by broquaint. I'm trying to get particular data from an array and he suggested using a hash, which works perfectly because now I can get other data from the hash when I need. However, I came across an unexpected result.

my %hash = map { chomp; split /,/ } <DATA>; print $hash{1006},$/; __DATA__ 1001,choochoo 1002,candycane 1003,sockpuppet 1004,choochoo 1005,candycane 1006,sockpuppet6 1007,foo 1008,bar
Works great! However, some of the data is actually set up like this:
__DATA__ 0001,choochoo 0002,candycane 0003,sockpuppet 0109,choochoo
This doesn't work with references that start with '0'. In fact the '0109' even gives the 'illegal octal digit' warning.

Can anyone shed some light on this? Possible workarounds? I can't change the way I receive the data. The "000's" are what I get.

peppiv

Comment on Hash references and illegal octal digits
Select or Download Code
Re: Hash references and illegal octal digits
by Mr. Muskrat (Abbot) on Dec 24, 2002 at 15:57 UTC

    It's all in how you attempt to access the hash keys...
    $hash{0109} results in the illegal octal digit warning but $hash{'0109'} does not. And the following works fine.

    #!/usr/bin/perl use strict; use warnings; my %hash = map { chomp; split /,/ } <DATA>; for (sort keys %hash) { print "$_ => $hash{$_}",$/; } __DATA__ 1001,choochoo 1002,candycane 1003,sockpuppet 1004,choochoo 1005,candycane 1006,sockpuppet6 1007,foo 1008,bar 0001,choochoo 0002,candycane 0003,sockpuppet 0109,choochoo

    Update: Doh! I forgot to tell you why! You see Perl treats all numbers that start with a 0 as octal (unless the 0 is followed by an x in which case it is treated as hexadecimal).

      Ah, yes. You is the genius!

      Works great mon!

      Supa Thanx,

      and Happy Holidays!

      peppiv

        Oops! One more breakdown. It appears the code
        my %hash = map { chomp; split /,/ } @array;
        doesn't play well with this data.

        __DATA__ 1001,choochoo 1002,candycane 1003,sockpuppet 1004,choochoo 1005,candycane 1006,sockpuppet6 1007, 1008,bar 0001,choochoo 0002,candycane 0003,sockpuppet 0109,choochoo

        Where 1007 doesn't have any value. I have no idea how to treat this. Does anyone have any ideas?

        peppiv

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-07-11 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (232 votes), past polls