could you use array or string as hash key in Perl?
A hash key in current Perl can *only* be a string.
If you want to use an array you have to specify your own array to
string conversion facility in a way that is suitable for the problem
at hand. A simple "@array" may be appropriate or totally wrong,
depending on the actual situation.
and then what if you modify the array or string internally?
"Internally" as opposed to... what?
i tried
@a = (3,4);
$h{@a} = "ha";
and it took @a as the number 2.
Of course, because the array is evaluated in a scalar context and in a
scalar context an array is its size.
You may want to use the reference to the array, in which case the key
will stay the same also if you modify the array later. You just have
to know that the key will *not* be the reference, but also in this
case, its stringification: i.e. do not hope to recover the array
directly from the key, although that is not what you were asking for.
Incidentally, if you want to, then there's a suitable module to do
exactly that.
cognac:~ [16:06:18]$ perl -MData:
umper -e '@a=3..4; print Dumper \
{ "@a" =>1, \@a => 2 }'
$VAR1 = \{
'ARRAY(0x814fe34)' => 2,
'3 4' => 1
};
cognac:~ [16:06:25]$ perl -le '@a=3..4; $h{"@a"}=3; push @a,1; \
print $h{"@a"} || "undef"'
undef
cognac:~ [16:06:33]$ perl -le '@a=3..4; $h{\@a}=3; push @a,1; \
print $h{\@a} || "undef"'
3
Michele