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

Dear PERL Monks
What is the best way to extract the SHORTEST, LONGEST and AVERAGE numerical key from a hash?

Replies are listed 'Best First'.
Re: Hash Profile
by ar0n (Priest) on Apr 24, 2001 at 01:43 UTC
    use List::Util qw(min max sum); my $min = min keys %hash; my $max = max keys %hash; my $avg = sum(keys %hash) / keys %hash; # Won't work: avg keys %hash;
    Untested, btw. I'm not sure what you mean with shortest and longest numerical key. I'll assume here you mean minimum and maximum. If you mean shortest string-wise, use minstr and maxstr instead.

    Update: Doh! There is no avg function. Code is updated to reflect that.

    ar0n ]

      use Math::VecStat qw(average); my $avg = average( keys %hash );
      For more stats, take PDL

      Jeroen
      "We are not alone"(FZ)

Re: Hash Profile
by spaz (Pilgrim) on Apr 24, 2001 at 02:56 UTC
    I'm surprised nobody else's HOMEWORK-dar has gone off.
    Is this your first Perl class or what?

    -- Dave
Re: Hash Profile
by satchboost (Scribe) on Apr 24, 2001 at 22:33 UTC
    Anonymous Monk:
    Use the keys keyword and you now have a list of the keys in the hash. This list will be in no particular order. (values will give you the values.) Now, just use whatever favorite method you have to find the min, max, and avg from an list. (You do know how to find the min, max, and avg from a list, right?)