#!perl use strict; &main(); sub main { my %hash1 = ( "george" => "salsa", "doug" => "apple", "steve" => "cantaloupe", "alex" => "blueberry", "andrew" => "ginger" ); my %hash2 = ( "william" => "cats", "robert" => "dogs", "randy" => "birds", "phillip" => "fish" ); my %hash3 = ( "amy" => "swims", "georgianne" => "runs", "susan" => "jogs", "stephanie" => "walks", "sarah" => "bicycles", "debbie" => "dances" ); print "The result of calling hash_value_sort on \%hash1 is\n"; &hash_value_sort( %hash1 ); print "\n"; print "The result of calling hash_value_sort on \%hash2 is\n"; &hash_value_sort( %hash2 ); print "\n"; print "The result of calling hash_value_sort on \%hash3 is\n"; &hash_value_sort( %hash3 ); print "\n"; my @keys_sorted_by_value1 = &get_sorted_keys( %hash1 ); my @keys_sorted_by_value2 = &get_sorted_keys( %hash2 ); my @keys_sorted_by_value3 = &get_sorted_keys( %hash3 ); print ( "The keys of \%hash1 sorted by their values are: ", ( join( ", ", @keys_sorted_by_value1 ) ), "\n" ); print ( "The keys of \%hash2 sorted by their values are: ", ( join( ", ", @keys_sorted_by_value2 ) ), "\n" ); print ( "The keys of \%hash3 sorted by their values are: ", ( join( ", ", @keys_sorted_by_value3 ) ), "\n" ); print "\n"; return; } sub hash_value_sort { my %h = ( @_ ); my $key; foreach $key ( sort { $h{$a} cmp $h{$b} } keys %h ) { print "$key - $h{$key}\n"; } return; } sub get_sorted_keys { my %h = ( @_ ); return sort { $h{$a} cmp $h{$b} } keys %h; }