Please, use strict, use warnings, and format/indent your code properly. You'll save yourself a LOT of headaches that way.

Now, that said, a hash can by its very nature only contain one value for the same key. So simply assigning to the same key more than once won't work, as you'll just overwrite any old value that might previously have been associated with that key.

However, the value can be a reference to an array (or any data structure, really). perldsc has more information on this, but here's how I might do it:

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; my %parsed = (); while(<DATA>) { chomp; my ($key, $values) = split "=", $_, 2; my @values = split ",", $values; $parsed{$key} = \@values; } foreach my $key (sort keys %parsed) { say "$key: "; foreach my $value (@{ $parsed{$key} }) { say "\t$value"; } } __DATA__ key1=value1 key2=value2,value3,value4,value4,value5,value6,value7,value8,value9,va +lue10 key3=value11,value12

This outputs:

$ perl 1119309.pl key1: value1 key2: value2 value3 value4 value4 value5 value6 value7 value8 value9 value10 key3: value11 value12 $

The crucial lines here are the following two:

$parsed{$key} = \@values; # ... foreach my $value (@{ $parsed{$key} }) {

The first of these takes a reference to @values (using the \ operator); the second takes the reference stored in $parsed{$key} and dereferences it, i.e. converts it back to to an array, by using the @{ ... } circumfix construct. (Since @ is the array sigil, this can mnemonically be thought of as encapsulating a certain something and presenting it as an array on the outside.)

I hope this'll get you started! If you have further questions, just ask.


In reply to Re^4: How to assign an array to a value in hash? by AppleFritter
in thread Re: How to assign an array to a value in hash? by vukotid

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.