in reply to Approximate Matching Key
Quick comment/update...pop grep { $_ <= $value } ( sort { $a <=> $b } keys %hash );
The best code to use is going to depend on both the size and the sparseness of your hash. Define M as the maximum hash value that you plan to have, n as the number of hash entries. The "start at X, check hash backwards" method will take O(M/n) assuming a good distribution, while my method above will take O(n)+O(n*log n) ~ O(n*log n). Thus, if you have a dense hash, the count-backwards will be faster, while grep/sort will be faster for a sparse hash. You'll probably want to benchmark both to see which method is best suited for what you need if you are going to be using this call frequently.
|
|---|