David Green
David Green

Reputation: 1234

Adding extra entries to a hash in Perl

I have a Perl script that reads a file and searches for 1 of 3 possible strings.

When it finds a line with a matching string, it splits the line on the spaces, picks one of the elements in the resulting array, then joins the line back together with + and adds that line to a hash.

Data structure is as follows:

The problem is extra entries are being added to the hash that have invalid keys. I have stripped the input of \r and wrote the captured lines to a file before they are added to the hash. I can't figure out why this is happening.

Here is the code:

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

Here is some of the input. The line I starred is the last line before the invalid hash entry, but there are more in the full output.

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

Here is the hash, including an invalid entry - the invalid entry is the last one.

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)

Upvotes: 1

Views: 287

Answers (1)

user3105222
user3105222

Reputation:

First, your entry is not invalid, it is a hashref within a hash and you inserted it with your $hash{$epoch}->{$key}. Maybe you wanted to use another hash than $hash for this?

Then, you should clean up the spacing characters for safety:

...
print "Captured Line is $line\n";

$line =~ s/\s+/ /g;
$line =~ s/^ | $//g;
my @values = split(" ", $line);
...

Or you can also do this:

...
print "Captured Line is $line\n";

$line =~ s/^\s+//;
my @values = split(/\s+/, $line);
...

Also, don't forget to close FILE.

Upvotes: 2

Related Questions