note
fruiture
<p>So you say you have multiple values for certain keys.</p>
<code>
key => value,value,value
</code>
<p>This directly translates to Perl:</p>
<code>
my %hash = (
key => [ value, value, value ],
...
);
</code>
<p>We call this a HoA, a hash of arrays as every value is another (anonymous) array-reference.</p>
<p>The creation of such a data structure is very easy with Perl as it has magic autovivification:</p>
<code>
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
</code>
<p>That's it. Perl is great. <em>update: see [perlref] and [perldata] of course.</em></p>
-- <br/>
http://[fruiture].de
241176
241176