For starters, perl does not consider e-149 to be a numeric value, so I wll assume you meant 1e-149.

Your question implies that perl would somehow distinguish between arithmetic on hash values and arithmetic on any other scalars, which is not the case. Whatever arithmetic works for $foo and $bar, or $foo[ 0 ] and $bar[ 0 ], works for $foo{ epsilon } and $bar{ epsilon }. In particular

% perl -wle '$x = "1e-149"; print $x + "2e-149"' 3e-149 % perl -wle '$hash{ x } = "1e-149"; print $hash{ x } + "2e-149"' 3e-149 % perl -wle '$hash{ x } = "1e-149"; print $hash{ x } + "2e-36"' 2e-36
...as expected.

Last thing is that by default1 perl never interprets + to mean string concatenation. (It has . for that.) This is true even when applied to string operands. In this case the strings get converted to numeric values and added. As shown in the one-liners above, if the strings make sense as numbers, the addition will make sense arithmetically. But even when the operands don't make sense as numbers, perl will be a sport about it and do something:

% perl -wle 'print( "x" + "y" )' Argument "y" isn't numeric in addition (+) at -e line 1. Argument "x" isn't numeric in addition (+) at -e line 1. 0

1I suppose one could overload + for a class to mean concatenation, but this is not default behavior.

the lowliest monk


In reply to Re: Adding hash values together by tlm
in thread Adding hash values together by MonkPaul

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.