数组中的散列的perl值 [英] Array in value of hash perl

查看:215
本文介绍了数组中的散列的perl值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能分配一个数组作为值在键参考:值对Perl中的哈希表?

Is it possible to assign the reference of an array as the value in the key : value pair of a hash table in perl?

推荐答案

是的。用反斜杠创建到数组的引用:

Yes it is. Create a reference to the array by using backslash:

$hash{key} = \@array;

请注意,这将链接到实际的数组,因此,如果你执行的改变,例如:

Note that this will link to the actual array, so if you perform a change such as:

$array[0] = "foo";

这也将意味着 $哈希{键} [0] 设置为foo的

如果这不是你想要的,你可以通过使用匿名数组引用复制值 [...]

If that is not what you want, you may copy the values by using an anonymous array reference [ ... ]:

$hash{key} = [ @array ];

此外,你不必去通过阵列,为了做到这一点。你可以简单地直接分配:

Moreover, you don't have to go through the array in order to do this. You can simply assign directly:

$hash{key} = [ qw(foo bar baz) ];

了解更多关于的perldoc perlref

这篇关于数组中的散列的perl值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆