duggles has asked for the wisdom of the Perl Monks concerning the following question:
Here's a bit of sample code and the output:
Output:#!/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 Jane +t 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
I tried to find via googling what the "3/8" and the "2/8" represent with no luck. I was really surprised I didn't get something like a memory location or something, like when you try to improperly de-reference an array. This is not a big problem, but I would consider any answer as a contribution to a struggling novice's education.
Thanks in advance!
btw - I'm running v5.10.1 built for MSWin32-x86-multi-thread on Windows XP sp3 if it matters...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: %hash print output formatting question
by Anonymous Monk on Oct 27, 2010 at 20:45 UTC | |
by toolic (Bishop) on Oct 27, 2010 at 21:02 UTC | |
by duggles (Acolyte) on Oct 27, 2010 at 22:33 UTC | |
by duggles (Acolyte) on Oct 27, 2010 at 22:30 UTC |