加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:34:21 所属栏目:大数据 来源:网络整理
导读:我试图序列化散列哈希值,然后反序列化它以获取哈希的原始散列.问题是每当我反序列化它时..附加一个自动生成的$var1例如. 原始哈希 %hash=(flintstones = { husband = "fred",pal = "barney",},jetsons = { husband = "george",wife = "jane","his boy" = "el
我试图序列化散列哈希值,然后反序列化它以获取哈希的原始散列.问题是每当我反序列化它时..附加一个自动生成的$var1例如.

原始哈希

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

出来了
?????$VAR1 = {
??????????‘simpsons’=> {
??????????????????????????‘kid’=> “巴特”,
??????????????????????????‘妻子’=> “玛吉”,
??????????????????????????‘丈夫’=> “本垒打”
????????????????????????},
??????????‘flintstones’=> {
?????????????????????????????‘丈夫’=> “弗雷德”,
?????????????????????????????‘pal’=> “巴尼”
???????????????????????????},
};

有没有什么办法可以得到没有$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",);

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','pal' => 'barney'
                           }
        };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读