简体   繁体   English

如何写出或读入数组的Perl哈希?

[英]How can I write out or read in a Perl hash of arrays?

I have a program in Perl I'm working on where I would need multiple keys, and a way of giving each key multiple values and follow that by being able to both read them in and write them out to an external file depending on if the key matches what the person enters into standard input. 我在Perl中有一个程序我正在处理我需要多个键的位置,以及为每个键赋予多个值的方法,并通过能够读取它们并将它们写入外部文件来实现,具体取决于是否键匹配人输入的标准输入。 I've looked across several sites and have found information somewhat useful in reading in hashes of arrays, but not writing them out, and I would also need to be able able to add to the array in the external file. 我查看了几个站点并发现信息在读取数组哈希时有些有用,但没有写出来,我还需要能够在外部文件中添加到数组中。

Is this possible? 这可能吗?

Edit: Is there a way that it could be done with starter Perl? 编辑:有没有办法可以用启动器Perl完成? I'm a beginner. 我是初学者。 Hashes of arrays seemed like the best way to make it work, but what I really need is a way to show multiple values for the same key while only showing the key once. 数组的哈希似乎是使其工作的最佳方式,但我真正需要的是一种方法来显示同一个键的多个值,同时只显示一次键。

Check out Data::Dumper . 查看Data :: Dumper

For instance, this microscopic script: 例如,这个微观脚本:

#!/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);

produces this output, which could clearly be edited: 生成此输出,可以清楚地编辑:

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

It can also be evaluated to read the data back into the program. 还可以对其进行评估以将数据读回程序。


Extending the example to show reading in as well as printing out...Note that the code is in two main blocks, and the only variable common between the blocks is the name of the file. 扩展示例以显示读入和打印输出...请注意,代码位于两个主要块中,块之间唯一的共同变量是文件名。

#!/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";
    }
}

The output from that script is: 该脚本的输出是:

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

Since you mentioned storing and retrieving the data, I would suggest a combination of Data::Dumper and Storable . 既然你提到了存储和检索数据,我会建议Data :: DumperStorable的组合。 For example: 例如:

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

Data::Dumper is a great way to display this data for an end user: Data :: Dumper是为最终用户显示此数据的好方法:

warn Dumper(\%test);

But for storing and retrieving, Storable makes this quite easy: 但是对于存储和检索,Storable使这很容易:

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

After the retrieve, you should find yourself with a hashref of the data from the original. 检索之后,您应该发现自己使用原始数据的hashref。

Check out the module XML::Simple . 查看XML :: Simple模块。 It has functions XMLout that turn a hash of arbitrary items into XML and then a function XMLin that does the reverse. 它具有将任意项的散列转换为XML的函数XMLout,然后是反向执行的函数XMLin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM