#!/usr/bin/perl -w my %hash; #==================================================== sub print_bare { print "\n\n"; print "print \%hash with nothing else (no formatting):\n"; print %hash; print "\n\n"; } sub print_formatted { print "print \%hash with other text:\n"; print "\%hash: " . %hash . "\n\n"; } sub print_sorted { print "print \%hash sorted:\n"; foreach my $key (sort { $hash{$b} cmp $hash{$a} } (keys(%hash))) { print "\$key:$key \$hash{$key}:$hash{$key} \n"; } print "\n\n"; } #==================================================== %hash = (' Charlie and Alice ' => ' can ', ' Fred and Ethyl ' => ' cannot ', ' Ernie and Betty ' => ' do not care ', ' Arnold and Janet ' => ' might '); print_bare; print_sorted; print_formatted; print "\n\n"; %hash = (' Charlie and Alice ' => ' can ', ' Fred and Ethyl ' => ' cannot '); print_bare; print_sorted; print_formatted; #### print %hash with nothing else (no formatting): Ernie and Betty do not care Fred and Ethyl cannot Arnold and Janet might Charlie and Alice can print %hash sorted: $key: Arnold and Janet $hash{ Arnold and Janet }: might $key: Ernie and Betty $hash{ Ernie and Betty }: do not care $key: Fred and Ethyl $hash{ Fred and Ethyl }: cannot $key: Charlie and Alice $hash{ Charlie and Alice }: can print %hash with other text: %hash: 3/8 print %hash with nothing else (no formatting): Fred and Ethyl cannot Charlie and Alice can print %hash sorted: $key: Fred and Ethyl $hash{ Fred and Ethyl }: cannot $key: Charlie and Alice $hash{ Charlie and Alice }: can print %hash with other text: %hash: 2/8