Here's a tied hash solution that caches min and max. It turns these lookups into an O(1) operation, but an O(n) penalty is paid on delete's of min and max keys. Of course, there's also a performance loss due to the tied interface, but we're CS people after all, and we don't concern ourselves with messy practicalities like that, right? =)

Yes indeed, this required considerably more code than I had anticipated...

update: Added tilly's optimization (doh!), so now delete's are overall an O(1) operation. Just don't go deleting the keys in sorted order :)
update2: Added dkubb's optimization to _min and _max routines... good call!

use strict; package MinMaxHash; sub HASH () { 0 }; sub MIN () { 1 }; sub MAX () { 2 }; sub new { my $class = shift; my $self = bless {}, $class; tie %$self, $class; $self; } sub minkey { my $self = shift; (tied %$self)->[MIN]; } sub maxkey { my $self = shift; (tied %$self)->[MAX]; } sub TIEHASH { bless [], shift; } sub STORE { my ($self, $key, $val) = @_; $self->[HASH]{$key} = $val; if (!defined $self->[MIN] or $key < $self->[MIN]) { $self->[MIN] = $key; } if (!defined $self->[MAX] or $key > $self->[MAX]) { $self->[MAX] = $key; } } sub FETCH { my ($self, $key) = @_; $self->[HASH]{$key}; } sub DELETE { my ($self, $key) = @_; delete $self->[HASH]{$key}; $self->[MIN] = _min(keys %{$self->[HASH]}) if $key == $self->[MIN]; $self->[MAX] = _max(keys %{$self->[HASH]}) if $key == $self->[MAX]; } sub CLEAR { my $self = shift; $self = []; } sub EXISTS { my ($self, $key) = @_; exists $self->[HASH]{$key}; } sub FIRSTKEY { my $self = shift; () = keys %{$self->[HASH]}; # reset iterator each %{$self->[HASH]}; } sub NEXTKEY { my $self = shift; each %{$self->[HASH]}; } # tilly^H^H^H^H^Hdkubbs's min & max subs... used only during DELETE's sub _max { my $max = shift; $max < $_ and $max = $_ for @_; $max; } sub _min { my $min = shift; $min > $_ and $min = $_ for @_; $min; } ### EXAMPLE ### package main; use Data::Dumper; my $h = new MinMaxHash; $h->{1} = 'foo'; $h->{2} = 'bar'; $h->{3} = 'baz'; $h->{10} = 'goop'; $h->{-1} = 'moo'; print "Minkey is ", $h->minkey, $/; print "Maxkey is ", $h->maxkey, $/; print Dumper $h;
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

In reply to Re: Returning the lowest key in a hash (or highest) by MeowChow
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.