in reply to Re: Hash Trouble
in thread Hash Trouble

Thank you for all of the information! The new code was missing the association of $val in the else statement so it was added and the new code looks like: $1 = time stamp $2 = error message $3 = unique identifer I am not sure if the time stamp information is going to be used.
while(<RPT>) { chomp; if (/^(\D+.*\d{4}\s+)(\D+.*)(\w{11})$/) { $key = $3; $msg = $2; fmt_rpt($msg); if (exists($analysis{$key})) { $analysis{$key} .= $msg; } else { $val = $msg; $analysis{$key} = $val; } } else { print "Program Error 2\n"; exit 1; }
For me I would rather build one hash table instead of multiple hash tables and then merging them. To me it seems like a waste what are your thoughts?

Replies are listed 'Best First'.
Re^3: Hash Trouble
by ikegami (Patriarch) on Nov 19, 2004 at 18:13 UTC
    The new code was missing the association of $val

    Not at all. There's no reason to do
    $val = $msg;
    $analysis{$key} = $val;
    when
    $analysis{$key} = $msg;
    suffices.

    For me I would rather build one hash table instead of multiple hash tables and then merging them. To me it seems like a waste what are your thoughts?

    To which multiple hashes are you refering? You mean if you if you were to keep the timestamp? You could do something like:

    while(<RPT>) { chomp; if (/^(\D+.*\d{4}\s+)(\D+.*)(\w{11})$/) { my $timestamp = $1; my $msg = $2; my $key = $3; fmt_rpt($msg); if (exists($analysis{$key})) { $analysis{$key}[1] .= $msg; } else { $analysis{$key} = [ $timestamp, $msg ]; } } else { print "Program Error 2\n"; exit 1; }

    See perllol for details. This would be a HoA (Hash of Arrays), which is similar to the mentioned AoA (Array of Arrays).

      You are right... my blunder. No what I meant about the Hash thing was someone building a hash table for each file. In my case there are multiple files so I would perfer looping through for example the log files from multiple days and make one hash table. This section of code will be used for analysis and once certain peg counts are reached, email the responsible part will the information. Thank you for all of your assistance and words of wisdom, Terry.
        oh! well that's pretty simple, and you have a few alternatives.
        { local @ARGV = ('file1', 'file2', ...); while (<>) { chomp; ... } }

        or

        foreach $filename ('file1', 'file2', ...) { local *RPT; open(RPT, '<', $filename) or die("...\n"); while (<RPT>) { chomp; ... } }

        or

        sub read_log { my ($filename, $analysis_ref) = @_; local *RPT; open(RPT, '<', $filename) or die("...\n"); while (<RPT>) { chomp; ... if (exists($$analysis_ref{$key})) { $$analysis_ref{$key} .= $msg; } else { $$analysis_ref{$key} = $msg; } ... } } my %analysis; read_log($_, \%analysis) foreach ('file1', 'file2', ...);