从Perl中的数组中读取哈希

时间:2016-09-01 13:03:05

标签: arrays perl csv hash

我是perl的新手(昨天晚上开始)。 我的哈希数组有问题。

以下情况: 我有一些来自日志记录应用程序的csv文件。日志文件是每天创建的,结构非常糟糕。我想将它们组合在一起并显示一些统计数据。 读取和解析csv确实不是问题,但我想将列存储在哈希中以便于访问。 在csv文件中,secound列(PhysName)是我想要对数据进行分组的列。所以我想,最好只保存哈希中的其他列,将数个哈希值保存在数组中并将数组保存在其他哈希中,其中我将PhysName作为键。

这根本不是什么大事:

my %dauerauftraege;

# Glob all CSV-Files in folder
my @files = glob("*.csv");

foreach my $file(@files) {
    # read the file
    open(DATA, "<$file") or die("Could not open File");

    LINE:
    foreach my $line(<DATA>) {
        chomp($line);
        if ($line eq "") {
            next LINE;
        }

        # Split Line into Fields
        my @line_data = split(";", $line);

        my $phys_name = $line_data[1];

        # skip the header file
        if ($phys_name eq "PhysName") {
            next LINE;
        }

        my %values =(
        'date_time' => $line_data[0],
        'sender' => $line_data[2],
        'recipient' => $line_data[3],
        'format' => $line_data[4],
        'transport' => $line_data[5],
        'partnername' => $line_data[6]);

        push(\@{$dauerauftraege{"$phys_name"}}, \%values);
    }
}

当我尝试使用print(Dumper(%dauerauftraege))验证时,我会收到以下信息:

$VAR1 = 'YYYYYYYXXXXXXXX';
$VAR2 = [
          {
            'transport' => 'FTP',
            'format' => 'V1',
            'partnername' => 'A_TEST',
            'date_time' => '2016.07.25 11:16:52',
            'sender' => 'BBB',
            'recipient' => 'CCC'
          },
          {
            'recipient' => 'CCC',
            'sender' => 'BBB',
            'partnername' => 'A_TEST',
            'date_time' => '2016.07.25 11:17:15',
            'format' => 'V1',
            'transport' => 'FTP'
          }
        ];
$VAR3 = 'XXXXXXXYYYYYYYY';
$VAR4 = [
          {
            'format' => 'V2',
            'partnername' => 'S_TEST',
            'date_time' => '2016.07.25 10:15:02',
            'recipient' => 'DDD',
            'sender' => 'AAA',
            'transport' => 'HTTP'
          },
          {
            'transport' => 'HTTP',
            'recipient' => 'DDD',
            'sender' => 'AAA',
            'partnername' => 'S_TEST',
            'format' => 'V2',
            'date_time' => '2016.07.25 10:15:30'
          }
        ];

我的第一个想法是,Dumper向我显示奇怪的$ VAR作为键,以及偶数$ VAR作为值,但在我仔细查看偶数$ VAR&中的哈希值之后#39; s,我非常不确定在将内部哈希(带有列和值)插入数组时出错了,存储在%dauerauftraege Hash中。

所以,现在谈到从Hash读取,包含一个带有此代码的哈希数组

# Get the keys to iterate over them
my @dauerauftraege_keys = keys(%dauerauftraege);
print("count dauerauftraege: ".(scalar @dauerauftraege_keys)."\n");
# iterate over the keys to get the array of hashes
foreach my $dauerauftrag_key (@dauerauftraege_keys) {
    # retrive the array
    my @dauerauftrag = $dauerauftraege{"$dauerauftrag_key"};
    print("count hashes inside the array: ".(scalar @dauerauftrag)."\n");
    # iterate over the hashes and print the values
    foreach my $dauerauftrag_values (@dauerauftrag) {
        print(%{$dauerauftra_values}{'date_time'});
        print("\n");
        print(%{$dauerauftra_values}{'sender'});
        print("\n");
        print(%{$dauerauftra_values}{'recipient'});
        print("\n");
        print(%{$dauerauftra_values}{'format'});
        print("\n");
        print(%{$dauerauftra_values}{'transport'});
        print("\n");
        print(%{$dauerauftra_values}{'partnername'});
        print("\n");
    }
}

我总是收到这个错误:

count dauerauftraege: 2
count hashes inside the array: 1
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 56, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 57, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 58, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 59, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 60, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 61, <DATA> line 5.
date_timesenderrecipientformattransportpartnernamecount hashes inside the array: 1
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 56, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 57, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 58, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 59, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 60, <DATA> line 5.
Use of uninitialized value in print at ./LastRun_Evaluation.pl line 61, <DATA> line 5.
date_timesenderrecipientformattransportpartnername

这让我相信,我的问题在于以下内容:

push(\@{$dauerauftraege{"$phys_name"}}, \%values);

当我仔细阅读这一行时,我理解以下内容: $dauerauftraege{"$phys_name"}返回一个标量($),其中包含对数组的引用,@{}将其解除引用到数组。 现在使用\%values我将对values Hash的引用传递给push Funktion,将其添加到数组的引用中,该引用由\表示前面的@。

如果有人能帮我解决这个问题,我会非常高兴。 谢谢大家: - )

3 个答案:

答案 0 :(得分:1)

看起来好像你不太了解如何取消引用数据结构。看看下面的代码,我已经改了一些,希望能让事情变得更清晰:

my @keys = keys(%dauerauftraege);

for my $key (@keys) {

    print "*** $key ***\n";

    my $aref = $dauerauftraege{$key};

    for my $values_href (@$aref){
        print "$values_href->{date_time}\n";
        print "$values_href->{sender}\n";
        print "$values_href->{recipient}\n";
        print "$values_href->{format}\n";
        print "$values_href->{transport}\n";
        print "$values_href->{partnername}\n";
    }
    print "\n";
}

请参阅perldsc

答案 1 :(得分:1)

我认为问题在于您如何访问数据结构。试试这个:

my @dauerauftraege_keys = keys %dauerauftraege;
print("count dauerauftraege: ".(scalar @dauerauftraege_keys)."\n");
# iterate over the keys to get the array of hashes
foreach my $dauerauftrag_key (@dauerauftraege_keys) {
    # retrieve the array
    my $dauerauftrag = $dauerauftraege{"$dauerauftrag_key"}; # ref to array
    my @a = @$dauerauftrag;
    print("count hashes inside the array: ".(scalar @a)."\n");
    # iterate over the hashes and print the values
    foreach my $dauerauftrag_values (@a) { # also a reference
        print $dauerauftrag_values->{'date_time'} . "\n";
        print $dauerauftrag_values->{'sender'} . "\n";
        print $dauerauftrag_values->{'recipient'} . "\n";
        print $dauerauftrag_values->{'format'} . "\n";
        print $dauerauftrag_values->{'transport'} . "\n"; 
        print $dauerauftrag_values->{'partnername'} . "\n";
    }
}

答案 2 :(得分:1)

始终使用编译指示use strict;use warnings;。它们有助于避免拼写错误和其他问题。

%dauerauftraege是一个哈希,你试图像数组一样访问它。并且您正在尝试通过哈希循环,同时在整个哈希的每次迭代上打印。

我认为代码看起来像这样:

use strict;
use warnings;
my %dauerauftraege;

# Glob all CSV-Files in folder
my @files = glob("*.csv");

foreach my $file(@files) {
    # read the file
    open(my $fh, "<", $file) or die("Could not open File");

    LINE:
    foreach my $line(<$fh>) {
        chomp($line);
        next LINE unless($line);

        # Split Line into Fields
        my @line_data = split(";", $line);

        my $phys_name = $line_data[1];

        # skip the header file
        next LINE if ($phys_name eq "PhysName");

        @{$dauerauftraege{"$phys_name"}}
          {'date_time', 'sender', 'recipient',
           'format', 'transport', 'partnername'}=@line_data[0,2..6];
    }
}

打印......

# Get the keys to iterate over them
my @dauerauftraege_keys = keys(%dauerauftraege);
print("count dauerauftraege: ".(scalar @dauerauftraege_keys)."\n");
# iterate over the keys to get the array of hashes
foreach my $dauerauftrag_key (@dauerauftraege_keys) {
    print "\n$dauerauftrag_key:\n";
    print join("\n",
          @{$dauerauftraege{"$dauerauftrag_key"}}{'date_time','sender','recipient',
                          'format','transport','partnername'})."\n";
}