my $k = {3,5,6,8}; my $y = {%$k};
为什么?那是做什么的?这似乎与此相同:
my $y = $k;
使用dbi模块调用上下文:
while (my $x = $db->fetchrow_hashref ) { $y{something} = {%$x}; }解决方法 不同之处在于它在不引用相同内存的情况下克隆数据结构.
例如:
use strict;use warnings;use Data::Dumper;my $h={'a'=>1,'b'=>2};my $exact_copy=$h; #$exact_copy references the same memory as $h$h->{b}++; #$h maps b to 3print Dumper($exact_copy) . "\n"; #a=>1,b=>3my $clone={%$h}; #We dereference $h and then make a new reference$h->{a}++; #h Now maps a to 2print Dumper($clone) . "\n"; #a=>1,b=>3 so this clone doesn't shadow $h
顺便说一下,使用所有逗号(如我的$k = {3,8})手动初始化哈希是非常非常难看的.
总结以上是内存溢出为你收集整理的perl hashref / perl语法全部内容,希望文章能够帮你解决perl hashref / perl语法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)