【问题标题】:How can I write out or read in a Perl hash of arrays?如何写出或读入 Perl 散列的数组?
【发布时间】:2010-09-27 06:36:00
【问题描述】:

我有一个 Perl 程序,我正在处理我需要多个键的地方,以及一种为每个键提供多个值的方法,并通过能够读取它们并将它们写出到外部文件来遵循这一点,具体取决于on 如果密钥与该人输入标准输入的内容匹配。我浏览了几个站点,发现一些信息在读取数组的哈希值时有些有用,但不能将它们写出来,而且我还需要能够在外部文件中添加到数组中。

这可能吗?

编辑: 有没有办法可以用初学者 Perl 来完成?我是初学者。数组散列似乎是使其工作的最佳方式,但我真正需要的是一种方法,可以为同一个键显示多个值,同时只显示一次。

【问题讨论】:

  • Data::Dumper 示例使用写入的数组引用哈希。您可以通过将方括号更改为圆括号(括号)来使其成为数组的散列。如果你想要一个带键的列表列表,这就是在 Perl 中实现的方法,不管你是不是初学者。
  • 我想我理解你的建议,但在这种情况下,如果外部文件不存在,程序需要能够创建它,并且它需要能够如果有新的标准输入,则创建新键。很抱歉没有在一开始就提供所有信息。

标签: perl arrays hash


【解决方案1】:

查看Data::Dumper

例如,这个微观脚本:

#!/bin/perl -w
use strict;
use Data::Dumper;

my(%hash);

$hash{key1} = [ 1, "b", "c" ];
$hash{key2} = [ 4.56, "g", "2008-12-16 19:10 -08:00" ];

print Dumper(\%hash);

产生这个输出,可以清楚地编辑:

$VAR1 = {
          'key2' => [
                      '4.56',
                      'g',
                      '2008-12-16 19:10 -08:00'
                    ],
          'key1' => [
                      1,
                      'b',
                      'c'
                    ]
        };

也可以通过评估将数据读回程序中。


扩展示例以显示读入和打印出...请注意,代码位于两个主要块中,块之间唯一共同的变量是文件名。

#!/bin/perl -w
use strict;
use Data::Dumper;
use FileHandle;

my $file = "data.out";

{
    my(%hash);

    $hash{key1} = [ 1, "b", "c" ];
    $hash{key2} = [ 4.56, "g", "2008-12-16 19:10 -08:00" ];

    my $str = Data::Dumper->Dump([ \%hash ], [ '$hashref' ]);
    print "Output: $str";

    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;

    foreach my $key (sort keys %hash)
    {
        print "$key: @{$hash{$key}}\n";
    }
}

该脚本的输出是:

Output: $hashref = {
         'key2' => [
                     '4.56',
                     'g',
                     '2008-12-16 19:10 -08:00'
                   ],
         'key1' => [
                     1,
                     'b',
                     'c'
                   ]
       };
Input: $hashref = {
         'key2' => [
                     '4.56',
                     'g',
                     '2008-12-16 19:10 -08:00'
                   ],
         'key1' => [
                     1,
                     'b',
                     'c'
                   ]
       };
key1: 1 b c
key2: 4.56 g 2008-12-16 19:10 -08:00

【讨论】:

    【解决方案2】:

    由于您提到存储和检索数据,我建议将Data::DumperStorable 结合使用。 例如:

    use Data::Dumper;
    use Storable;
    my %test = (this => ['that', 'the', 'other'],
                that => ['this', 'the', 'other']
                );
    

    Data::Dumper 是向最终用户显示此数据的好方法:

    warn Dumper(\%test);
    

    但是对于存储和检索,Storable 使这变得非常容易:

    # Save the hash to a file:
    store \%test, 'file.txt';
    # Retrieve the hash from the file.
    my $testref = retrieve('file.txt');
    

    检索后,您应该会发现自己拥有原始数据的 hashref。

    【讨论】:

    • 您可能需要 nstore() 以防涉及数字。 n 版本按网络顺序存储东西。
    【解决方案3】:

    查看模块XML::Simple。它有函数 XMLout 将任意项的哈希转换为 XML,然后有一个函数 XMLin 进行相反的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-19
      • 2014-01-16
      • 1970-01-01
      • 2010-12-31
      • 2016-08-03
      • 1970-01-01
      • 2012-08-03
      • 2017-02-21
      相关资源
      最近更新 更多