I had written some Perl code to go through thousands of files and match for errors contained in a hash shown in <span style="color: rgb(153, 51, 0);">brown</span> (see <b>Old code</b> section). I disliked this approach because I didnt want to alter the core program everytime I added or removed search strings/expressions. My goal is to read the list of strings from a text file and create the same hash using a loop as shown in <span style="color: rgb(51, 102, 255);">blue</span> (see <b>New code</b> section).<br>
<br>The problem with this approach is that attempts to print the values for any keys of this hash are unsuccessful and show up as blanks. I ran it through a debugger and saw this instead with the help of Padwalker:<br><br>
<span style="color: rgb(0, 102, 0);">$erl_hash HASH(0xa541e88)=... </span><br style="color: rgb(0, 102, 0);"><span style="color: rgb(0, 102, 0);"> ArithmeticException CODE(0xa5493a8) </span><br style="color: rgb(0, 102, 0);">
<span style="color: rgb(0, 102, 0);"> ArrayStoreException CODE(0xa549a88) </span><br style="color: rgb(0, 102, 0);"><br>I was expecting to see something more like this:<br><span style="color: rgb(0, 102, 0);">$erl_hash HASH(0xa541e88)=... </span><br style="color: rgb(0, 102, 0);">
<span style="color: rgb(0, 102, 0);"> ArithmeticException sub {return ArithmeticException}</span><br style="color: rgb(0, 102, 0);">
<span style="color: rgb(0, 102, 0);"> ArrayStoreException sub {return </span><span style="color: rgb(0, 102, 0);">ArrayStoreException</span><span style="color: rgb(0, 102, 0);">}</span><br><br>Any help would be appreciated. I can share the entire program if need be.<br>
<br>Regards,<br>Amit<br><br><br><br><u><b>Old code</b></u><br><span style="color: rgb(153, 51, 0);"># Create the Hash for matching Errors</span><br style="color: rgb(153, 51, 0);"><span style="color: rgb(153, 51, 0);">my $exception_match = {</span><br style="color: rgb(153, 51, 0);">
<span style="color: rgb(153, 51, 0);"> 'ArithmeticException' => sub {return "ArithmeticException"},</span><br style="color: rgb(153, 51, 0);"><span style="color: rgb(153, 51, 0);"> 'ArrayStoreException' => sub {return "ArrayStoreException"},</span><br style="color: rgb(153, 51, 0);">
<span style="color: rgb(153, 51, 0);">};</span><br style="color: rgb(153, 51, 0);"><br><br>#Error Matching Section<br><span style="color: rgb(255, 0, 0);">my $match_err = Regexp::Assemble->new( track => 1 )->add( keys %$exception_match );</span><br style="color: rgb(255, 0, 0);">
<br>my %error_counter = (); #This hash stores the total count of all errors<br><br>foreach my $filename (@logFile_list) {<br> # First Determine if we need to use DBM to handle file parsing<br>
my $filesize = stat($filename)->size;<br> $Logger->debug("File name is $filename\tFile Size is $filesize");<br> <br> # Next Determine if file is gzip compressed<br> my $gzipTest = qx(file $filename);<br>
if($gzipTest =~ m/gzip/i) {<br> open(MYINPUTFILE, "<:gzip", "$filename") or $Logger->logdie("Error opening file: $!");<br> } else {<br> open(MYINPUTFILE, "<$filename") or $Logger->logdie("Error opening file: $!");<br>
}<br><br># This section sends a message to the log file and STDOUT if an exception is caught <br> my $lines = 0;<br> <br> while( <MYINPUTFILE> )<br> {<br> $lines++;<br> chomp;<br> <span style="color: rgb(255, 0, 0);"> if( $match_err->match($_) )</span><br>
{<br> my ($exception_name) = $exception_match->{ $match_err->matched }( $match_err->mvar() );<br> $Logger->debug("Exception was found: $exception_name\tin file: $filename at line: $lines");<br>
$error_counter{$filename}{$exception_name}++; #Logic for counting the various types of errors <br> }<br><br> }<br>}<br><br><br><u><b>New code</b></u><br>#This section of code reads the err file and saves the key-value pairs in a hash for easy retrieval<br>
<span style="color: rgb(51, 102, 255);">my $erl_hash = {};</span><br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);">my $erl_file = $ini_hash{'error_list_file'};</span><br style="color: rgb(51, 102, 255);">
<br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);">open(FILE, "<", $erl_file) or die "Can't open $erl_file: $!\n";</span><br style="color: rgb(51, 102, 255);"><br style="color: rgb(51, 102, 255);">
<span style="color: rgb(51, 102, 255);">while (my $erl_file_line = <FILE>)</span><br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);"> {</span><br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);"> chomp $erl_file_line;</span><br style="color: rgb(51, 102, 255);">
<span style="color: rgb(51, 102, 255);"> if (($erl_file_line !~ m/^[\s]*#/gi) && ($erl_file_line !~ m/^[\s]*$/gi ))</span><br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);"> {</span><br style="color: rgb(51, 102, 255);">
<span style="color: rgb(51, 102, 255);"> $erl_file_line =~ m/^[\s]*([\w\S]+)[\s]*$/gi;</span><br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);"> $erl_hash->{$1} = sub {return $1};</span><br style="color: rgb(51, 102, 255);">
<span style="color: rgb(51, 102, 255);"> print "Added key: ",$1,"\tCorresponding value: ",$erl_hash->{$1},"\n";</span><br> }<br> }<br><br>#Dumper (%{$erl_hash});<br> <br>
#Error Matching Section<br><span style="color: rgb(255, 0, 0);">my $match_err = Regexp::Assemble->new( track => 1 )->add( keys %$erl_hash );</span><br><br><br>my %error_counter = (); #This hash stores the total count of all errors<br>
<br><br>foreach my $filename (@logFile_list) {<br> # First Determine if we need to use DBM to handle file parsing<br> my $filesize = stat($filename)->size;<br> $Logger->debug("File name is $filename\tFile Size is $filesize");<br>
<br> # Next Determine if file is gzip compressed<br> my $gzipTest = qx(file $filename);<br> if($gzipTest =~ m/gzip/i) {<br> open(MYINPUTFILE, "<:gzip", "$filename") or $Logger->logdie("Error opening file: $!");<br>
} else {<br> open(MYINPUTFILE, "<$filename") or $Logger->logdie("Error opening file: $!");<br> }<br> <br> # This section sends a message to the log file and STDOUT if an exception is caught <br>
my $lines = 0;<br> <br> while( <MYINPUTFILE> )<br> {<br> $lines++;<br> chomp;<br> <span style="color: rgb(255, 0, 0);"> if( $match_err->match($_) )</span><br> {<br> <span style="color: rgb(255, 0, 0);"> my ($exception_name) = $erl_hash->{ $match_err->matched }( $match_err->mvar() );</span><br>
$Logger->debug("Exception was found: $exception_name\tin file: $filename at line: $lines");<br> $error_counter{$filename}{$exception_name}++; #Logic for counting the various types of errors <br>
}<br><br> }<br>}<br>