Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Perl文件中存储和读取哈希和数组_Arrays_Perl_File_Hash_Save - Fatal编程技术网

Arrays 在Perl文件中存储和读取哈希和数组

Arrays 在Perl文件中存储和读取哈希和数组,arrays,perl,file,hash,save,Arrays,Perl,File,Hash,Save,我是一个noob。我需要一些关于如何在perl下保存和读取数据的基本知识。比如说保存一个散列和一个数组。应该使用什么格式(扩展名)的文件?txt?到目前为止,我只能将所有内容保存为stringprint FILE%hash,并将它们作为stringprint读回。如果我需要从文件中输入函数散列和数组,该怎么办。如何将它们放回散列和数组?您正在查找数据序列化。健壮的流行选择有,和。鲜为人知的格式有: 其他经常提到的选项是and(或类似)/eval,但我不能推荐它们,因为Storable的格式依赖于

我是一个noob。我需要一些关于如何在perl下保存和读取数据的基本知识。比如说保存一个散列和一个数组。应该使用什么格式(扩展名)的文件?txt?到目前为止,我只能将所有内容保存为string
print FILE%hash
,并将它们作为string
print
读回。如果我需要从文件中输入函数散列和数组,该怎么办。如何将它们放回散列和数组?

您正在查找数据序列化。健壮的流行选择有,和。鲜为人知的格式有:

其他经常提到的选项是and(或类似)/
eval
,但我不能推荐它们,因为Storable的格式依赖于Perl版本,而
eval
由于执行任意代码而不安全。截至2012年,解析计数器部分尚未取得很大进展。我也不推荐使用XML,因为它不能很好地映射Perl数据类型,并且存在多个相互竞争/不兼容的模式,如何在XML和数据之间进行转换


代码示例(已测试):



下一步是



另请阅读:

Perlmonks对序列化进行了两次很好的讨论


这实际上取决于您希望如何在文件中存储数据。我将尝试编写一些基本的perl代码,使您能够将文件读入数组,或将哈希写回文件

#Load a file into a hash.
#My Text file has the following format.
#field1=value1
#field2=value2  
#<FILE1> is an opens a sample txt file in read-only mode.
my %hash;
while (<FILE1>)
{
  chomp;
  my ($key, $val) = split /=/;
  $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
}
#将文件加载到散列中。
#我的文本文件的格式如下。
#字段1=值1
#字段2=值2
#是以只读模式打开示例txt文件的。
我的%hash;
而()
{
咀嚼;
我的($key,$val)=拆分/=;
$hash{$key}.=存在$hash{$key}?,$val:$val;
}

如果你是新手,我建议用join()从数组/散列中生成字符串,他们用“print”编写,然后读取并使用split()再次生成数组/散列。这将是一种更简单的方法,就像Perl教学课本示例一样。

您可以查看诸如或之类的模块。Data::Dumper应该帮助您按原样存储数组/哈希。您可以通过require/evalYeah重新使用它们。使用现有的CPAN模块比使用原始代码更合适。对不起。我想把它作为一个评论贴在那里。有趣又有用。这种方法在某些模块不容易安装的环境中很有价值。我同意首选的方法是使用Data::Dumper(近年来发展很快)或其他模块。缺点是什么?可能很难处理比数组或哈希(例如多维数组)更复杂的问题。真正的问题始于数据可以是任何字符串,包括包含分隔符符号。
use YAML::XS qw(Load Dump);
use File::Slurp qw(read_file write_file);
my %hash;
{
    my $yaml = Dump \%hash;
    write_file('dump.yml', { binmode => ':raw' }, $yaml);
}
{
    my $yaml = read_file('dump.yml', { binmode => ':raw' });
    %hash = %{ Load $yaml };
}
#Load a file into a hash.
#My Text file has the following format.
#field1=value1
#field2=value2  
#<FILE1> is an opens a sample txt file in read-only mode.
my %hash;
while (<FILE1>)
{
  chomp;
  my ($key, $val) = split /=/;
  $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
}