perl hash 参考的参考
my $hash_ref = { a => 1, b => 2 };
my $tmp_ref = $hash_ref;
代码如上,我想更改哈希值并插入一些新的对。我的问题如下:
- 我如何通过
$tmp_ref
实现这些 - 是否可以通过引用的引用更改或插入?
- 引用的引用、引用和具体的数据结构(这里是hash)之间是否一致?
多谢!
my $hash_ref = { a => 1, b => 2 };
my $tmp_ref = $hash_ref;
The code is as above and I want to change hash's value and insert some new pairs. my question are as follows:
- how can i achieve these by
$tmp_ref
- Is it possible to change or insert by reference's reference?
- Is it consistent among reference's reference, reference and concrete data structure (here is hash)?
thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,
$tmp_ref
不是对$hash_ref
的引用,它只是$hash_ref
值的副本。您可以使用
$tmp_ref
访问哈希,就像使用$hash_ref
一样:如果您确实想让
$tmp_ref
成为对的引用>$hash_ref
,以下是访问原始哈希值的方法:In this case,
$tmp_ref
is not a reference to$hash_ref
, it is simply a copy of whatever$hash_ref
's value is.You can access the hash with
$tmp_ref
like you would with$hash_ref
:In case you actually wanted to make
$tmp_ref
a reference to$hash_ref
, here is how you'd access the original hash:$hash_ref
和$tmp_ref
都将引用相同的哈希值,因此您可以向$hash_ref
添加一些内容:然后两个
$hash_ref
和$tmp_ref
将点引用相同的(a => 1, b => 2, c => 3)< /代码>哈希。
引用是 Perl 版本的指针。
Both
$hash_ref
and$tmp_ref
will refer to the same hash so you can add something to$hash_ref
with:Then both
$hash_ref
and$tmp_ref
willpointrefer to the same(a => 1, b => 2, c => 3)
hash.References are Perl's version of pointers.