Note that I have no idea on the answer to this, as it's not intuitively following from what is out there in perldata.

In reference to a recent question on multi-keyed hashes, I got to wondering exactly what the perl hash could take. We know that :

my %hash; @hash{ 3,4 } = qw( perl hacker ); print join ",", keys %hash; # prints "3,4"
Since that is your hash slice. But I got to wondering, what if you accidently called this in scalar form, that is:
my %hash; $hash{ 3,4 } = qw( perl hacker ); print join ",", keys %hash; # ????
I initially thought that the comma operator, forcing output as a list, would be done first, and thus the hash step would convert the list to a scalar (or in this case, '2') and use that as the key, but I was wrong. When I ran this, I got "34", that is, there was a single key in the hash. But this was on linux; on windows (both 5.005 and 5.6.1), I got "34" (if that doesn't come out correctly, the middle symbol looks like a reduced capital L.) A bit more digging shows up that linux actually has an invisible symbol there, represented by less as '^\'. Weirrrd.

Thinking that the hash might be interpreting this strangely, I tried the following:

my %hash; $hash{ (3,4) } = qw( perl hacker ); print join ",", keys %hash; # ????
Again, the same result occured.

Obviously, going to :

my %hash; $hash{ [3,4] } = qw( perl hacker ); print join ",", keys %hash; # ????
gives "ARRAY(0xDEADBEEF)" or some such like that, which isn't a surprise at all.

Was this reliable?

my %hash; $hash{ 3,4 } = qw( perl hacker ); print $hash{ 3,4 }, "\n";
Which results in "hacker". Very interesting...

Variables work with no problem:

my %hash; for my $i (0..5) { for my $j (0..5) { $hash{ $i,$j } = $i*$j; } } foreach ( keys %hash ) { print "$_ : ", $hash{ $_ }, "\n"; }
This code prints out the correct multiplication with each 'key'. This also works with any number of 'keys', eg expanding the above to $i, $j, $k.

So I have tumbled accidently into a way to do multidimensional hashes? I know this isn't the most elegant solution, but something seems to be working. Would it be possible to split on this odd key, such that one can make grep { nth_element_of_the_key eq 'test' } keys %hash;


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to An interesting oddity with hashes by Masem

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.