in reply to Sorting question
Maybe something like this? (Contrived example)
use warnings; use strict; my %hash = ( 'Bob' => { 'best' => 9, 'worst' => 5 }, 'Steve' => { 'best' => 5, 'worst' => 1 }, 'Mark' => { 'best' => 10, 'worst' => 4 }, 'Dave' => { 'best' => 7, 'worst' => 3 } ); print "\npeople by best\n"; for my $key ( sort_by( \%hash, 'best' ) ) { print "$key\n" } print "\npeople by worst\n"; for my $key ( sort_by( \%hash, 'worst' ) ) { print "$key\n" } print "\nproperties\n"; for my $key ( sort_by( $hash{'Bob'}, undef ) ) { print "$key\n" } sub sort_by { my ( $ref, $constant ) = @_; if ( defined $constant ) { return sort { $ref->{$b}->{$constant} <=> $ref->{$a}->{$consta +nt} } keys %$ref; } else { return sort { $ref->{$b} <=> $ref->{$a} } keys %$ref; } }
|
|---|