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

如何将HASH 存入文件 然后将文件转为hash

发布时间:2020-12-15 20:50:01 所属栏目:大数据 来源:网络整理
导读:perl Hash 我比较喜欢的,好多数据我都是存在 hash中。 但是有些数据我是不想他,程序退出后不保存的。所以想把数据存入文件中。 这个东西研究了 好多个小时,想找一个和 SHELL 语言一样的 文件读写方式。 ? 经过 google 很一下。发现有几个方案是比较好的。

perl Hash 我比较喜欢的,好多数据我都是存在 hash中。

但是有些数据我是不想他,程序退出后不保存的。所以想把数据存入文件中。

这个东西研究了 好多个小时,想找一个和 SHELL 语言一样的 >> << 文件读写方式。

?

经过 google 很一下。发现有几个方案是比较好的。

?

方案一Dump :

#!/bin/perl -w
use strict;
use Data::Dump 'dump';
use FileHandle;
my $file = "data.out";

{
?my %hash = ("apples"=>17,
????"bananas"=>9,
????"oranges"=>"none");
?my $str = Data::Dumper->Dump([ /%hash ],[ '$hashref' ]);
?my $out = new FileHandle ">$file";
?print $out $str;
?close $out;
}

{
?my $in = new FileHandle "<$file";
?local($/) = "";
?my $str? = <$in>;
?close $in;
?#print "Input: $str";
?my($hashref);
?eval $str;
?my %hash = %$hashref;
?print dump(/%hash),"/n";
}

?

?

方案2:使用Storable

?

#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Storable;
my %test = (this => ['that','the','other','323232'],
???that => ['this','121122']
???);
warn Dumper(/%test);
# Save the hash to a file:
store /%test,'file.txt';
# Retrieve the hash from the file.
my $testref1 = retrieve('file.txt');
warn Dumper($testref1);

?

方案3:XML ()

没有细细研究

(编辑:李大同)

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

    推荐文章
      热点阅读