This is a great post by Grandfather!
There is an important part he illustrated that might get missed...notice that there are 8 hash assignment statements, but only 7 lines in printout. What happened? The $hash{10} = 'ten'; statement caused the numeric ten to be converted to string for use as a hash key. The $hash{'10'} = "'ten'"; statement generated the same key and caused 10's value to be overwritten.

So what that means is that if you use a numeric value as a hash key, Perl will convert it to a string. There will not be any leading zeroes in that string!

The poster looks like he wants a table to translate a number into text months. I suspect that putting '08' in quotes is unlikely to be the best thing here and that the first reply "get rid of the leading zero" is probably better because I suspect this "8" will come from a numeric routine (like a time function) and if it doesn't (like from a webpage, etc) there is any easy way to "get rid of leading zeroes".

In Perl everything is a string until used in a numeric context. The below shows one trick that I sometimes use to "delete leading zeroes" without regex...See example 1, this just adds "0" to the string! Example 2 shows some pitfalls of comparing numeric values as strings! Example 3 show's Grandfather's 8 vs 7 keys with another example. The hash lookup needs a string and makes a string if it needs to for use in calculating the actual binary hash key.

#EXAMPLE 1 my $a = "0001"; print "a 1 as string is:$a\n"; $a+=0; # A trick! forces numeric context!!! print "a 1 as number is:$a\n"; #EXAMPLE 2 my $b="01"; if ($a eq $b) { print "$a is eq $b\n"; } else { print "$a is NOT eq $b\n"; } if ($a == $b) { print "$a is == $b\n"; } else { print "$a is NOT == $b\n"; } #EAMPLE 3 my $string_eight="8"; my $numeric_eight = 8;
if ($string_eight eq $b) { print "string $string_eight is eq number $numeric_eight\n"; } else { print "string $string_eight is NOT eq number $numeric_eight\n"; }
my %hash = ('8'=>"string eight", 8 =>'number 8'); print "hash key uses the stringified version of 8: $hash{'8'} or $hash +{$numeric_eight}\n"; __END__ a 1 as string is:0001 a 1 as number is:1 1 is NOT eq 01 1 is == 01
#string 8 is NOT eq number 8</strike>
hash key uses the stringified version of 8: number 8 or number 8

In reply to Re^2: Illegal octal digit error by Marshall
in thread Illegal octal digit error by lakshmananindia

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.