[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] print "%hash";



Jay Hannah wrote:
I hit this code today

  $body .= "Email Address: ".$$results{email}."%0D%0A" if ($$results{email});

and thought "What? Isn't Perl going to try to interpret "%0D" as a hash?

Nope.

"%hash" is not interpreted at all. It's just a string.

Fun with hashes below...

j


$ cat j.pl
my %j = ( a => 1, b => 2 );
print %j, "\n";
print %j . "\n";
print "%j\n";

$ perl j.pl
a1b2
2/8
%j


perldoc perldata
   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.  This
   is pretty much useful only to find out whether Perl's
   internal hashing algorithm is performing poorly on your
   data set.  For example, you stick 10,000 things in a hash,
   but evaluating %HASH in scalar context reveals "1/16",
   which means only one out of sixteen buckets has been
   touched, and presumably contains all 10,000 of your items.
   This isn't supposed to happen.

_______________________________________________
Omaha-pm mailing list
Omaha-pm@pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm

That's rather unusual but very useful, I don't know any other language that has built in hashing functionality to have that sort of instrumentation (regarding allocated buckets versus full buckets). I never knew that! Awesome stuff, I like how Perl pretty intelligently guesses what you want to know, which is nice. Thanks for bringing this up Jay.

Best Regards,
Travis