I have written two small functions (that are really the same thing just reworded) to return the lowest key and highest key in a hash. I get the feeling I am doing a lot of work (well, a moderate amount of work) to do something perl probably already does for me.
So without further ado, the code:
# assume a hash that looks like this: %hash = ( 1 => $hashref, 2 => $anotherhashref ); sub least_key { my %nums = %{ shift }; shift (@{ sort { $a <=> $b } keys (%nums) }); } sub high_key { my %nums = %{ shift }; shift (@{ sort { $b <=> $a } keys (%nums) }); }
Im aware that instead of switching $a and $b, i could have used pop instead of shift, but that isnt really relevant. I also thought this would be interesting to look at because its a perfect example of where perl's syntax gets a little murky.
psst, this isnt a round of perl golf either. :) I am just wondering if there is a better way to do this.
--
Laziness, Impatience, Hubris, and Generosity.
In reply to Returning the lowest key in a hash (or highest) by deprecated
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |