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 ( ) { chomp; $stat_info->{ADD}($_); } print join "\t", map { $_->() } @{$stat_info}{qw/MAX MIN AVE TOT/};