Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: minimum, maximum and average of a list of numbers at the same time

by Limbic~Region (Chancellor)
on Nov 10, 2005 at 13:45 UTC ( [id://507374]=note: print w/replies, xml ) Need Help??


in reply to Re^2: minimum, maximum and average of a list of numbers at the same time
in thread minimum, maximum and average of a list of numbers at the same time

And if you want to go even more FP...
sub Gen_Stats { my $stat = {}; my ($cnt, $max, $min, $tot); $stat->{ADD} = sub { $cnt += @_; for ( @_ ) { $tot += $_; $max = $_ if ! defined $max || $_ > $max; $min = $_ if ! defined $min || $_ < $min; } }; $stat->{MAX} = sub { $max }; $stat->{MIN} = sub { $min }; $stat->{AVE} = sub { $cnt ? $tot / $cnt : undef }; $stat->{TOT} = sub { $tot }; $stat->{ADD}->( @_ ); return $stat; } my $stat_info = Gen_Stats(); while ( <DATA> ) { chomp; $stat_info->{ADD}($_); } print join "\t", map { $_->() } @{$stat_info}{qw/MAX MIN AVE TOT/};
This code was borrowed from RFC: Tool::Box. I recommend using List::Util when and wherever possible. It has been part of the core since 5.007003 and uses XS when possible. The only real limitation I see with it is that all the items in the list must be known at once as I pointed out in How A Function Becomes Higher Order.

Cheers - L~R

  • Comment on Re^3: minimum, maximum and average of a list of numbers at the same time
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://507374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found