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

In reply to %hash print output formatting question by duggles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.