List Info

Thread: Could arrays be used as hash keys?




Could arrays be used as hash keys?
country flaguser name
United States
2008-03-11 17:28:11

Hello All:

I was wondering whether it is possible to have arrays as hash keys. For example:

%hash = { ["a", "b"] => ["c", "d"] };

I came across hash slices while searching for this, but they seem to map individual keys to individual values.

I'd appreciate any help in this regard.
Thanks
Aditi

---------------------------------
Forgot the famous last words? Access your message archive online. Click here.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: Could arrays be used as hash keys?
country flaguser name
United States
2008-03-11 18:09:28

>>&gt;>> "aditi" == aditi gupta < aditi9783%40yahoo.co.in">aditi9783yahoo.co.in> writes:

aditi>; I was wondering whether it is possible to have arrays as hash keys. For example:

No.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<; merlyn%40stonehenge.com">merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

__._,_.___
.

__,_._,___
Re: Could arrays be used as hash keys?
country flaguser name
Czech Republic
2008-03-12 09:04:11

To: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
From: aditi gupta < aditi9783%40yahoo.co.in">aditi9783yahoo.co.in>
Date sent: Tue, 11 Mar 2008 22:28:11 +0000 (GMT)
Subject: [PBML] Could arrays be used as hash keys?
Send reply to: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com

> Hello All:
>;
> I was wondering whether it is possible to have arrays as hash keys. For example:
>
> %hash = { ["a&quot;, "b&quot;] => ["c&quot;, "d&quot;] };
>
> I came across hash slices while searching for this, but they seem to map individual keys to individual values.
>
> I'd appreciate any help in this regard.
> Thanks
&gt; Aditi

A quick search on http://search.cpan.org reveals those:

http://search.cpan.org/~osfameron/Tie-Hash-StructKeyed-
0.03/lib/Tie/Hash/StructKeyed.pm

http://search.cpan.org/~snowhare/Tie-ListKeyedHash-
1.02/lib/Tie/ListKeyedHash.pod

http://search.cpan.org/~nuffin/Tie-RefHash-1.38/lib/Tie/RefHash.pm

Please keep in mind that
[1,2] != [1,2]
because each [] created a brand new reference to a new array!
Which causes the Tie::Refhash to behave differently than
Tie::ListKeyedHash or Tie::Hash::Structkeyed!

Jenda
===== Jenda%40Krynicky.cz">JendaKrynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

__._,_.___
.

__,_._,___
Re: Could arrays be used as hash keys?
country flaguser name
United States
2008-03-13 14:30:29

--- In perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com, aditi gupta <aditi9783...> wrote:
&gt;
> I was wondering whether it is possible to have arrays as hash keys.
For example:
>
> %hash = { ["a&quot;, "b&quot;] => ["c&quot;, "d&quot;] };

Hashes (and arrays) can only hold scalars, that is, a string, a number
or a reference. So in your example the hash will look like (if dumped
with Data:umper):

$VAR1 = {
'ARRAY(0x804e15c)' => [
'c',
'd'
]
};

Of course, that number is quite random, and, more importantly, if you
tried to overwrite it like this:

$hash{[&quot;a", "b&quot;]} = 'something else';

you would actually insert one more pair with some other random
reference instead:

$VAR1 = {
'ARRAY(0x804e15c)' => [
'c',
'd'
],
'ARRAY(0x809add4)' => 'something else'
};

/sandy http://myperlquiz.com/

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )