I see solutions to your problem, but since I don't see any explanation, I'll provide one.

Your problems lies with the fact that hashes can only have a single value at any given key. However, those value can be an array (or hash) reference which may contain a list of items.

One provided solution provided is:
push @{$fruits{$a}},$b
which is equivalent to:
$fruits{$a} = [] unless $fruits{$a}; # create array, save ref
push @{$fruits{$a}},$b

People suggested you could also form a string representing the list of values:
if (defined($fruits{$a})) {
   $fruits{$a} .= ','.$b;  # Add to existing list
} else {
   $fruits{$a} = $b;       # Start new list
}
This isn't good if you're planning on manipulating the individual values, but it good if you're just going to print out the list without modification.

One of the Perl docs refers to Array of Arrays (AoA). You should read up on those.


In reply to Re: How to store this value in Hash ? by ikegami
in thread How to store this value in Hash ? by gube

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.