The problem is you can't have two keys with the same name because the keys of hash must be unique. So when you get the second "10", the value will override the first. The technique you need is putting together the values for the same key in an array reference. You don't have to check whether a key already exists if you want them to be in the same format, so as shown in previous replies, just treat the value of as array reference, dereference it, and push the new value. You may find perlreftut, perlref, and perldsc as rich resources.

I'd like to also suggest you to lexicalize the fields variables within the while loop scope, since you don't need them outside of the loop.

In more verbose:

my %count; while (...) { my($field1, $field2) = split(...); # assume that we already has the key $field1 in %count # so the value would be $count{$field1}. # # assume it to be array reference, pretend you did: # $count{$field1} = [] # # the push() function requires real array so you need # to dereference it first, like this @{$count{$field1}}, # then push the new value # push @{$count{$field1}}, $field2; }

However, if you want to keep the unique keys to be scalar instead of array reference, you have to check for keys existence.

my %count; while (...) { my($field1, $field2) = split(...); if (exists $count{$field1}) { # force the value to be array ref if it's # not done so $count{$field1} = [$count{$field1}] unless ref $count{$field1}; push @{$count{$field1}}, $field2; } else { $count{$field1} = $field2; # normal assignment } }

On more thing, for simple dumping, I would rather use the straightwoard Dumper() function:

print Dumper(\%count);

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: working with a hash by naikonta
in thread working with a hash by semio

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.