在perl中初始化哈希数组

时间:2014-12-09 12:35:25

标签: perl

如果我写了

my %modules = (
Paths => ["$path","$other_path"]
);

我可以通过

访问
print $modules{Paths}[1];

但我会像

一样初始化它
my @rray = ("$path", "$other_path");
my %modules = ( Paths => @rray);

但是它不适用于

print $modules{Paths}[1];

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您需要引用@rray数组。

my %modules = (Paths => \@rray);

您可能需要查看perldoc perlreftutperldoc perldsc

my @rray = ("$path", "$other_path");
my %modules = ( Paths => @rray);

会导致键/值对的列表变平,

my %modules = ("Paths", "$path", "$other_path");
#              key1      value1  key2

在这种特殊情况下,会在警告下警告Odd number of elements in hash assignment

相关问题