Perl does not solve this problem particularly cleanly, but it can be arranged to.

Since Perl is list-oriented, a design that allows you to think in terms of lists is likely to play well with the rest of how the language is organized. To this end List::Util offers handy functions like max and min, with which you can just type this:

my $least = min(keys %hash); my $largest = max(keys %hash);
This is nice because you solve your problem with hashes, but it also works for any other kind of list. And since Perl passes data by reference, this is fairly efficient.

Now I suggested this in chatter, but unfortunately you had trouble compiling it. But these functions still are not hard to write in native Perl:

sub max { my $max = shift; $max = ($max < $_) ? $_ : $max foreach @_; return $max; }
and min is similar. Plus the corresponding string functions. (The fact that you need to have these pairs of operations - and would need more if you tried adding more basic data types - is something that has been bugging me about Perl lately. Ah well, no language is perfect.)

For a generalization of the problem we also have reduce, which can be implemented in native Perl like this:

sub reduce (&@) { my $op = shift; no strict 'refs'; my $pkg = caller; local *A = local *{"$pkg\::a"}; $A = shift; local *B = local *{"$pkg\::b"}; foreach $B (@_) { $A = $op->(); } $A; }
(Yes, this uses prototypes. And globals.) Now with this single (icky) definition out of the way a lot of list utilities can be written fairly easily. Consider:
sub max { reduce {$a > $b ? $a : $b } @_; }
which as you can see is sort of like the flexibility of sort except for gathering things in a list together, not rearranging them.

Another example to give a sense of what reduce is like:

sub sum { reduce {$a + $b} @_; }

UPDATE
Cleaned up the implementation of reduce slightly.


In reply to Re (tilly) 1: Returning the lowest key in a hash (or highest) by tilly
in thread Returning the lowest key in a hash (or highest) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.