Here's another idea: use tie.

To explain myself some more:

Create a module, in there you create a hash, and make the tie method accept a param called 'rounded', which has a true/false value.

Then tie it to two hashes, in your example, you tie the hash %stats to the module, with the rounded param set to false, and tie the hash %rounded_stats to the same module, but with the rounded param set to true.

Then implement the FETCH, and VALUES method to check the value of the rounded-param...

This would have the advantage that your data is only stored once (this also means it is in sync at all time), but it would also mean that fetching a value might be a bit slower/less efficient (since it has to do the sprintf)...

I can't tell what would be best, creating a real hash vs tieing it to two hashes, since that really depends on how you are planning on using it...

(References: `perldoc -f tie`, `perldoc perltie`, and `perldoc Tie::Hash`)


In reply to Re: rounding off all members of a hash by Animator
in thread rounding off all members of a hash by vindaloo

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.