In reference to a recent question on multi-keyed hashes, I got to wondering exactly what the perl hash could take. We know that :
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; # prints "3,4"
my %hash; $hash{ 3,4 } = qw( perl hacker ); print join ",", keys %hash; # ????
Thinking that the hash might be interpreting this strangely, I tried the following:
Again, the same result occured.my %hash; $hash{ (3,4) } = qw( perl hacker ); print join ",", keys %hash; # ????
Obviously, going to :
gives "ARRAY(0xDEADBEEF)" or some such like that, which isn't a surprise at all.my %hash; $hash{ [3,4] } = qw( perl hacker ); print join ",", keys %hash; # ????
Was this reliable?
Which results in "hacker". Very interesting...my %hash; $hash{ 3,4 } = qw( perl hacker ); print $hash{ 3,4 }, "\n";
Variables work with no problem:
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.my %hash; for my $i (0..5) { for my $j (0..5) { $hash{ $i,$j } = $i*$j; } } foreach ( keys %hash ) { print "$_ : ", $hash{ $_ }, "\n"; }
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: An interesting oddity with hashes
by wog (Curate) on Jun 02, 2001 at 07:09 UTC | |
|
Re: An interesting oddity with hashes
by chipmunk (Parson) on Jun 03, 2001 at 00:58 UTC | |
|
(tye)Re: An interesting oddity with hashes
by tye (Sage) on Jun 02, 2001 at 10:39 UTC | |
|
Re: An interesting oddity with hashes
by Vynce (Friar) on Jun 02, 2001 at 12:14 UTC |