Hi, I am new to perl programming and I am trying to figure out how to upda +te a hash in perl. I have the following file that I would like to rea +d line by line. OA322 U383 Mar 3.84 2.44 OA322 U982 Dec 2.57 1.83 OA322 U946 May 2.87 1.35 OA322 U823 Dec 6.61 4.08 OA322 U590 Aug 8.19 9.62 OA322 U19 Jul 9.93 9.23 BD110 U144 Apr 4.22 4.93 BD110 U848 Dec 1.72 7.30 BD110 U498 Jun 6.88 8.72 BD110 U606 Sep 5.54 9.86 BD110 U16 Jul 6.47 7.67 BD110 U160 Sep 7.21 4.12 BD110 U572 Jul 2.60 6.86 BD110 U804 Jul 0.04 9.71 RF858 U261 Jan 5.69 1.64 RF858 U731 Sep 9.45 7.13 RF858 U551 Jan 4.67 6.40 RF858 U888 Dec 7.22 4.20 VS524 U509 Jan 5.94 9.46 VS524 U14 Aug 5.22 1.78 MN739 U710 Jul 3.98 1.83 I would like to create a hash that looks like this. Given below is the + script. What is happening is that my hash only has the last line it +reads for each key. Appreciate any help !!
foreach (@new_line) { print $_, "\n"; my @ju = split ,@new_line; $self->{code}{$ju[0]} = { event => $ju[1], month => $ju[2], am => $ju[3], pm => $ju[4], } } print Dumper $self->{code}; } Output: $VAR1 = { 'MN739' => { 'am' => '3.98', 'event' => 'U710', 'month' => 'Jul', 'pm' => '1.83' }, 'RF858' => { 'event' => 'U888', 'am' => '7.22', 'pm' => '4.20', 'month' => 'Dec' }, 'VS524' => { 'month' => 'Aug', 'pm' => '1.78', 'am' => '5.22', 'event' => 'U14' }, 'BD110' => { 'event' => 'U804', 'am' => '0.04', 'pm' => '9.71', 'month' => 'Jul' }, 'OA322' => { 'month' => 'Jul', 'pm' => '9.23', 'am' => '9.93', 'event' => 'U19' } };

In reply to Question on updating a hash table by TJ-47

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.