in reply to Hash Trouble
chop should be chomp.
"$3" should be $3.
"$msg" should be$msg.
"$val" should be $val.
Why are you using a substitution instead of if ($_ =~ /^(\D+.*\d{4}\s+)(\D+.*)(\w{11})$/?
$_ =~ can be omitted.
It's bad to use $1, $2, etc after calling a function after they've been set.
fmt_rpt($msg) should probably be $msg = fmt_rpt($msg).
And the answer to your question: $val .= "$msg"; should $analysis{$key} .= $msg. I'm not sure if that's exactly what you want, but you never save $val into the hash, so your problem is definitely here.
$val is used but never set in the else part of the if.
And the answer to your question: $val .= "$msg"; should $analysis{$key} .= $msg. I'm not sure if that's exactly what you want, but you never save $val into the hash, so your problem is definitely here.$1 is never used. I'm going to leave the capture in the code below, in case you plan on using $1.
Fixed code:
while(<RPT>) { chomp; if (/^(\D+.*\d{4}\s+)(\D+.*)(\w{11})$/) { $key = $3; $msg = $2; # Maybe should be: $msg = fmt_rpt($msg); fmt_rpt($msg); if (exists($analysis{$key})) { $analysis{$key} .= $msg; } else { $analysis{$key} = $msg; } } else { print "Program Error 2\n"; exit 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash Trouble
by tc1364 (Beadle) on Nov 19, 2004 at 18:04 UTC | |
by ikegami (Patriarch) on Nov 19, 2004 at 18:13 UTC | |
by tc1364 (Beadle) on Nov 19, 2004 at 18:55 UTC | |
by ikegami (Patriarch) on Nov 19, 2004 at 19:09 UTC |