I have two comments concerning the solution you chose.
First, don't use prototypes unless necessary. They cause a lot of headaches.
Second, you can simplify your code by handling sorting and ascending/descending seperately.
sub Print_Tests { my $sort = shift || "a"; my $order = shift || "d"; my (@ordered_keys); # Determine the order. if ($sort eq 't') { @ordered_keys = sort { $tests{$a}{"Total"} <=> $tests{$b}{"Total"} + } keys %tests; } elsif ($sort eq 'v')) { @ordered_keys = sort { $tests{$a}{"Value"} <=> $tests{$b}{"Value"} + } keys %tests; } else { @ordered_keys = sort keys %tests; } @ordered_keys = reverse @ordered_keys if $order eq 'd'; foreach my $key (@ordered_keys) { my $val = $tests{$key}; print "$key\n"; print " Total: $val->{Total}\n"; print " Value: $val->{Value}\n\n"; } }
In reply to Re: Sorting a hash of a hash using anonymous subs
by ikegami
in thread Sorting a hash of a hash using anonymous subs
by madbombX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |