任何perl专家都可以帮助我理解这段perl代码。
$a=18;
$b=55;
$c=16;
$d=88;
$mtk = {
'A' => [$a, $b],
'M' => [$c, $d]
};这个字典包含字符和对,以及如何访问键和值,非常感谢
发布于 2013-01-17 03:37:28
$a、$b、$c和$d是scalars。$mtk是hash of arrayrefs的reference。您可以像这样访问它:
print $mtk->{A}[0]; ## 18如果您刚刚开始并为此代码而苦苦挣扎,我建议您阅读这本书。
perldoc perlreftut
发布于 2013-01-17 03:46:11
这是作为值的数组引用的哈希引用。下面是一段遍历代码:
for my $key (sort keys %$mtk) {
print "Current key is $key\n";
for my $val (@{ $mtk->{$key} }) {
print "... and one of value is $val\n";
}
}输出将是
Current key is A
... and one of value is 18
... and one of value is 55
Current key is M
... and one of value is 16
... and one of value is 88https://stackoverflow.com/questions/14366168
复制相似问题