duggles has asked for the wisdom of the Perl Monks concerning the following question:

I discovered a bit of strangeness quite by accident (actually by bad programming technique). I was doing some quick and dirty work and wanting to print out a hash without "foreach-ing" my way through it. It's not a big problem, but I would like to know why I'm getting the output I'm getting.

Here's a bit of sample code and the 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;
Output:
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...

Life is short, but it's wide -- Chuck Pyle

Replies are listed 'Best First'.
Re: %hash print output formatting question
by Anonymous Monk on Oct 27, 2010 at 20:45 UTC
    It's right in the docs :)

    "If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash."
      The OP can avoid evaluating the hash in scalar context by changing the dots to commas in the print statement:
      sub print_formatted { print "print \%hash with other text:\n"; print "\%hash: " , %hash , "\n\n"; }

      For quick dumping of hashes (and arrays, and references, etc.), see also Data::Dumper.

        makes perfect sense... Thanks for the tip on using commas instead!!

        fyi - I'm a big fan of Data::Dumper, but in this case I was being a bit lazy...

        Thanks again!
        Life is short, but it's wide -- Chuck Pyle

      makes perfect sense... I get a little frustrated trying to find things in the docs - I do remember (a long time ago) reading something about the buckets though....

      I appreciate you pointing me in the right direction!

      Life is short, but it's wide -- Chuck Pyle