gpt4 book ai didi

Perl 序列化和反序列化散列的散列

转载 作者:行者123 更新时间:2023-12-04 17:02:33 24 4
gpt4 key购买 nike

我试图序列化散列的散列,然后反序列化它以取回散列的原始散列..问题是每当我反序列化它..它附加一个自动生成的 $var1 例如。

原始哈希

%hash=(flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
);

出来作为
$VAR1 = {
'辛普森' => {
' child ' => '巴特',
'妻子' => '玛姬',
'丈夫' => '本垒打'
},
'打火石' => {
'丈夫' => '弗雷德',
' friend ' => '巴尼'
},
};

有什么方法可以在没有 $var1 的情况下获得原始哈希值??

最佳答案

你已经证明 Storable 工作得非常好。 $VAR1是 Data::Dumper 序列化的一部分。

use Storable     qw( freeze thaw );
use Data::Dumper qw( Dumper );

my %hash1 = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
);

my %hash2 = %{thaw(freeze(\%hash1))};

print(Dumper(\%hash1));
print(Dumper(\%hash2));

如您所见,原件和副本是相同的:
$VAR1 = {
'jetsons' => {
'his boy' => 'elroy',
'wife' => 'jane',
'husband' => 'george'
},
'flintstones' => {
'husband' => 'fred',
'pal' => 'barney'
}
};
$VAR1 = {
'jetsons' => {
'his boy' => 'elroy',
'wife' => 'jane',
'husband' => 'george'
},
'flintstones' => {
'husband' => 'fred',
'pal' => 'barney'
}
};

关于Perl 序列化和反序列化散列的散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com