在 Perl 中向哈希添加额外条目

问题描述 投票:0回答:1

我有一个 Perl 脚本,它读取文件并搜索 3 个可能字符串中的 1 个。

当它找到具有匹配字符串的行时,它会在空格上分割该行,选择结果数组中的一个元素,然后用 + 将该行重新连接在一起,并将该行添加到哈希中。

数据结构如下:

  • 关键是整行。
  • 该值是保存的数组的元素,在本例中是日期。

问题是额外的条目被添加到具有无效键的哈希中。我已经剥离了

\r
的输入,并将捕获的行写入文件,然后再将其添加到哈希中。我不明白为什么会发生这种情况。

这是代码:

sub ProcessInput() {
    my $word;
    foreach $word (@wantLines) {
        if ($DEBUG) { print( $word, ":\t", $line, "\n" ); }
        if ( $line =~ /$word/ ) { 
            if (   $word eq "C3_TIMEOUT_FRAME"
                || $word eq "RX_WT_AVG_B2B_ZERO"
                || $word eq "TX_WT_AVG_B2B_ZERO" )
            {   
                if ($DEBUG) { print $word, "\n"; }

                # store the counter in the hash
                # the key is the entire line where spaces are replaced with '*'
                #  this is the only way to guarantee a unique key
                print "Captured Line is $line\n";

                my @values = split( " ", $line );
                my $key = join( '*', @values );
                my $epoch = parsedate( $values[3] );    #, GMT=>1);
                open( FILE, ">$outfile" ) || die "Can't open file ($!)\n";

                # unless (exists $hash{$epoch}->{$key})
                unless ( exists $hash{$key} ) { 

                    # print "Added key $key to the hash\n";
                    $hash{$epoch}->{$key} = $line;

               # add the entry to the hash, but only if it doesn't exist already
               # the value is the date for the log entry
               # unless (exists $hash{$key})

                    $hash{$key} = $values[3];
                }    #unless

            }    #if $word
        }    #if $DEBUG
    }    #foreach

这是一些输入。我加星标的行是无效哈希条目之前的最后一行,但完整输出中还有更多行。

fc1/11             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |199      |06/16/13 21:34:58
fc1/9              |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |802      |06/16/13 21:16:52
**fc1/15             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |1588     |06/16/13 20:32:49**
fc1/15             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |1587     |06/16/13 17:28:10
fc1/15             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |1586     |06/16/13 16:29:41
fc1/11             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |198      |06/16/13 13:17:30
fc1/37             |AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO  |1025     |06/16/13 11:41:20

这是哈希值,包括无效条目 - 无效条目是最后一个条目。

fc1/15*AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO*1600*06/22/13*06:50:25 => 06/22/13
fc1/5*AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO*125*06/17/13*07:39:40 => 06/17/13
fc1/9*AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO*832*06/26/13*00:02:09 => 06/26/13
fc1/11*AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO*209*06/21/13*09:26:09 => 06/21/13
fc1/15*AK_FCP_CNTR_RX_WT_AVG_B2B_ZERO*1588*06/16/13*20:32:49 => 06/16/13
1370923200 => HASH(0x97d5a0)
perl hash
1个回答
2
投票

请删除此数据。谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.