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; }

In reply to Re: Hash Trouble by ikegami
in thread Hash Trouble by tc1364

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.