in reply to Approximate Matching Key

Here is one way - the hash I use is not a dbm hash, but it is trivial to port it:
my %h = ( 8 => 'value', 3 => 'value', 102 => 'value', 4 => 'value', 1 => 'value', 57 => 'value', ); my @keys = sort { $a <=> $b } keys %h; my $try = shift || 5; # this example allows user to specify on comm +and line my $low = 'not found'; foreach (@keys) { $low = $_ if $_ < $try; last if $_ >= $try; } print "$low\n";

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

Replies are listed 'Best First'.
Re: (jeffa) Re: Approximate Matching Key
by baku (Scribe) on Apr 07, 2001 at 00:36 UTC

    Minor typo...

    foreach (@keys) { $low = $_ if $_ <= $try; last if $_ >= $try; }

    The = was missing in the $low line, so it would miss exact matches. It should still be present on the last line as well, despite my first instinct :-), because we can guarantee to have found a match.