Please send questions to
st10@humboldt.edu .
#!/usr/bin/perl -w
#######################################################
# lect08_dbm3
#
# now display the contents of the score_db DBM file(s)
# highest-score first, in ASCIIbetical order
# of key for those with the same score.
#
# modified by Sharon Tuttle from "Learning Perl",
# by Schartz and Phoenix
#
# last modified: 10-12-04
#######################################################
# open an existing OR create a new DBM file, accessible
# via a DBM hash
# (note --- same DBM file -- but I can call the DBM hash
# variable different names in different scripts, if
# I want to...)
dbmopen(%SCORES, "score_db", 0644)
or die "Cannot create score_db: $!";
# let's show the contents highest-score to lowest,
# with ties shown in ASCIIbetical order by key
#-----------------------------------------------------
# SORT SUBROUTINE,
# so, since it involves a particular hash, it IS
# using a GLOBAL variable, and we've placed it
# AFTER we know that the hash exists
#---------------------------------------------------
sub score_sort
{
$SCORES{ $b } <=> $SCORES{ $a }
or
$a cmp $b
}
# HMMM --- SHOULD use while and each to more EFFICENTLY
# access hash (in terms of memory efficiency) ---
# BUT I don't see how I would sort the values, then...?
print "\n";
foreach my $scorer (sort score_sort keys %SCORES)
{
printf "%-10s %5d\n", $scorer, $SCORES{ $scorer };
}
print "\n";
# end of lect08_dbm3