Please send questions to
st10@humboldt.edu .
#!/usr/bin/perl -w
#######################################################
# lect08_sort_mult_keys
#
# simple example sorting by multiple keys in a hash
#
# modified by Sharon Tuttle from "Learning Perl",
# by Schartz and Phoenix
#
# last modified: 10-11-04
#######################################################
my %score = ( "wilma" => 195,
"fred" => 205,
"dino" => 30,
"barney" => 195
);
#------------------------------------------------------
# sort by descending numeric score, BUT if two
# players have the SAME score, sort ASCIIbetically
# by name
#-----------------------------------------------------
sub by_score_and_name
{
# HMMMM --- no real way that I can see yet, in these
# little sort subroutines, to AVOID "global"
# use of hash --- that's an EXCEPTION to our
# subroutine style standard to avoid globals!!!
# (that is, we'll say such globals are OKAY in sort
# subroutines...)
$score{$b} <=> $score{$a}
or
$a cmp $b
}
#-------------------------------------------------------------
# action starts here!
#-------------------------------------------------------------
my @winners = sort by_score_and_name keys %score;
my $player;
print "\n";
foreach (@winners)
{
$player = $_;
printf "%-8s %5d\n", $player, $score{$player};
}
print "\n";
# end of lect08_sort_mult_keys