Sort::Hash::Values - sort hashes by values
use Sort::Hash::Values; my %birth_dates = ( Larry => 1954, Randal => 1961, Damian => 1964, Simon => 1978, Mark => 1965, Jesse => 1976, ); for my $name (sort_values { $a <=> $b } %birth_dates) { printf "%7s was born in %s.\n", $name, $birth_dates{$name}; }
sort_values() is a function that returns keys of values after sorting its values.
sort_values()
All functions are exported using Exporter. If you don't want this (but why you would use this module then) try importing it using empty list of functions.
use Sort::Hash::Values ();
The only function in this module. It sorts every value in hash using specified code and returns list of their keys sorted according to their values.
Just like with sort in Perl, when code prototype is $$, the variables to sort will be in @_ too.
sort
$$
@_
When giving the function to sort_values it has to be function in scope where you call sort_values. This is internal limitation caused by the fact that Perl module cannot know what package the function you used belongs. It can only know where sort_values was called. This bug also affects reduce in List::Util, pairwise in List::MoreUtils and other functions that use $a or $b variables - those variables have to be modified in function's package.
sort_values
reduce
pairwise
$a
$b
The code block isn't optional. I really would like to make it optional, but I cannot with Perl limitations. Instead, use { $a cmp $b } just after sort_values.
{ $a cmp $b }
If you will make $a or $b lexical, except this module to break, as they aren't referencing global variables anymore. This affects every function that uses those variables, even sort builtin. Just don't.
When using $a or $b only once in the code (with the exception for sort builtin), Perl will warn you. This also affects other modules that use those variables. To remove warnings about this, use following code.
no warnings 'once';
Konrad Borowski <glitchmr@myopera.com>
This software is copyright (c) 2012 by Konrad Borowski.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Sort::Hash::Values, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sort::Hash::Values
CPAN shell
perl -MCPAN -e shell install Sort::Hash::Values
For more information on module installation, please visit the detailed CPAN module installation guide.