小编TVN*_*ack的帖子

绑定两个Perl哈希值(不是两个匿名哈希值)

为了重用一些已编写的代码库,我需要将hash(%bar)伪装成另一个hash(%foo),以便在绑定%bar可以看到所做的每个更改.%foo

使用匿名哈希构造,很容易:只需将ref复制barfoo ($foo = $bar;)的ref中.

如何"不使用"匿名哈希(因为我必须重用的代码不使用匿名哈希构造)?这是唯一可能的吗?

谢谢.

use strict;
use Data::Dumper;

my  %foo = ();
my  %bar = ();

%foo = %bar;    # This won't work as it copies %bar into %foo as at the current time.

$bar{A}->{st} = 'a';
$bar{A}->{qt} = 'a';
$bar{B}->{st} = 'b';
$bar{B}->{qt} = 'b';

# $foo{A}->{st}  doesn't exist
Run Code Online (Sandbox Code Playgroud)

当然,匿名哈希构造本来就是一种祝福.

use strict;
use Data::Dumper;

my  $foo = {};
my  $bar = {};

$foo …
Run Code Online (Sandbox Code Playgroud)

perl hash

1
推荐指数
1
解决办法
56
查看次数

标签 统计

hash ×1

perl ×1