Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently using the List::Utils module to calculate the min and max of several time spans. I now need to calculate the average times...Is there a way to do this with the List::Utils module or am I forced to use something else?

Thanks

Replies are listed 'Best First'.
Re: Average Time?
by Fletch (Bishop) on Jan 27, 2009 at 15:44 UTC

    Other than the obvious use List::Utils qw( sum ); my $avg = sum( @spans ) / @spans;? If that's not it perhaps provide some sample data . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      That was obvious...but unfortunately my time format is: H:M:S so I guess I could convert the time to seconds, do the calc, and convert back...I was just being lazy!

      Thanks

Re: Average Time?
by JavaFan (Canon) on Jan 27, 2009 at 15:45 UTC
    use List::Util 'sum'; my $avg = sum(@times) / @times; # Dies if no times.

      And the same with error handling:

      use List::Util 'sum'; my $avg = @times ? sum(@times)/@times : "NaN";

      As I just discovered w/ some help on CB, don't expect the above "NaN" to be NaN on MS. s/"NaN"/1e9999\/1e9999/ for portable (and less readable) support there.

      Update: ikegami is of course right that it's a C lib problem, not OS dependent, but that does not change that there are potential NaN portability issues.

        NaN support is based on the underlying C lib, not the OS.