vindaloo has asked for the wisdom of the Perl Monks concerning the following question:
I want to round off all the values in a given hash to 2 decimal places. I do not want to mess with the original, given hash. This is what I came up with:
use strict; use warnings; use diagnostics; use Data::Dumper::Simple; my %stats = ( 'stdev' => '42.5371995604396', 'pct_10' => 0, 'variance' => '668.660986666667', 'sterr' => '17.3657390016697', 'median' => '-130.925', 'pct_90' => '94.4', 'average' => '131.503333333333' ); print Dumper(%stats); my %rounded_stats = (); foreach my $stat (keys %stats){ #print "$stat $stats{$stat}\n"; my $round = sprintf("%.2f", $stats{$stat}); $rounded_stats{$stat} = $round; } print Dumper(%stats); print Dumper(%rounded_stats);
Is this the best way? Any advice or tips?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: rounding off all members of a hash
by Zaxo (Archbishop) on May 04, 2005 at 03:51 UTC | |
by qef (Initiate) on May 04, 2005 at 08:09 UTC | |
by vindaloo (Acolyte) on May 04, 2005 at 04:02 UTC | |
|
Re: rounding off all members of a hash (for values)
by tye (Sage) on May 04, 2005 at 05:09 UTC | |
|
Re: rounding off all members of a hash
by eibwen (Friar) on May 04, 2005 at 05:03 UTC | |
|
Re: rounding off all members of a hash
by Animator (Hermit) on May 04, 2005 at 08:39 UTC | |
by Keystroke (Scribe) on May 04, 2005 at 18:56 UTC |