So you say you have multiple values for certain keys.
key => value,value,value
This directly translates to Perl:
my %hash = (
key => [ value, value, value ],
...
);
We call this a HoA, a hash of arrays as every value is another (anonymous) array-reference.
The creation of such a data structure is very easy with Perl as it has magic autovivification:
use Data::Dumper qw/Dumper/;
my %hash = ();
while( <DATA> ){
chomp;
my ($k,$v) = split /\s*:\s*/;
push @{ $hash{ $k } } , $v
}
print Dumper( \%hash );
__DATA__
green : apple
yellow: banana
green : kiwi
red : strawberry
green : mango
yellow: lemon
That's it. Perl is great. update: see perlref and perldata of course.
--
http:// fruiture.de
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|