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);">    &#39;ArithmeticException&#39; =&gt; sub {return &quot;ArithmeticException&quot;},</span><br style="color: rgb(153, 51, 0);"><span style="color: rgb(153, 51, 0);">    &#39;ArrayStoreException&#39; =&gt; sub {return &quot;ArrayStoreException&quot;},</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-&gt;new( track =&gt; 1 )-&gt;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)-&gt;size;<br>    $Logger-&gt;debug(&quot;File name is $filename\tFile Size is $filesize&quot;);<br>    <br>    # Next Determine if file is gzip compressed<br>    my $gzipTest = qx(file $filename);<br>
    if($gzipTest =~ m/gzip/i) {<br>        open(MYINPUTFILE, &quot;&lt;:gzip&quot;, &quot;$filename&quot;) or $Logger-&gt;logdie(&quot;Error opening file: $!&quot;);<br>    } else {<br>        open(MYINPUTFILE, &quot;&lt;$filename&quot;) or $Logger-&gt;logdie(&quot;Error opening file: $!&quot;);<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( &lt;MYINPUTFILE&gt; )<br>    {<br>        $lines++;<br>        chomp;<br>      <span style="color: rgb(255, 0, 0);">  if( $match_err-&gt;match($_) )</span><br>
        {<br>            my ($exception_name) = $exception_match-&gt;{ $match_err-&gt;matched }( $match_err-&gt;mvar() );<br>            $Logger-&gt;debug(&quot;Exception was found: $exception_name\tin file: $filename at line: $lines&quot;);<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{&#39;error_list_file&#39;};</span><br style="color: rgb(51, 102, 255);">
<br style="color: rgb(51, 102, 255);"><span style="color: rgb(51, 102, 255);">open(FILE, &quot;&lt;&quot;, $erl_file) or die &quot;Can&#39;t open $erl_file: $!\n&quot;;</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 = &lt;FILE&gt;)</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) &amp;&amp; ($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-&gt;{$1} = sub {return $1};</span><br style="color: rgb(51, 102, 255);">
<span style="color: rgb(51, 102, 255);">            print &quot;Added key: &quot;,$1,&quot;\tCorresponding value: &quot;,$erl_hash-&gt;{$1},&quot;\n&quot;;</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-&gt;new( track =&gt; 1 )-&gt;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)-&gt;size;<br>    $Logger-&gt;debug(&quot;File name is $filename\tFile Size is $filesize&quot;);<br>
    <br>    # Next Determine if file is gzip compressed<br>    my $gzipTest = qx(file $filename);<br>    if($gzipTest =~ m/gzip/i) {<br>        open(MYINPUTFILE, &quot;&lt;:gzip&quot;, &quot;$filename&quot;) or $Logger-&gt;logdie(&quot;Error opening file: $!&quot;);<br>
    } else {<br>        open(MYINPUTFILE, &quot;&lt;$filename&quot;) or $Logger-&gt;logdie(&quot;Error opening file: $!&quot;);<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( &lt;MYINPUTFILE&gt; )<br>    {<br>        $lines++;<br>        chomp;<br>    <span style="color: rgb(255, 0, 0);">    if( $match_err-&gt;match($_) )</span><br>        {<br>  <span style="color: rgb(255, 0, 0);">          my ($exception_name) = $erl_hash-&gt;{ $match_err-&gt;matched }( $match_err-&gt;mvar() );</span><br>
            $Logger-&gt;debug(&quot;Exception was found: $exception_name\tin file: $filename at line: $lines&quot;);<br>            $error_counter{$filename}{$exception_name}++;                #Logic for counting the various types of errors            <br>
        }<br><br>    }<br>}<br>