Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: arrays from arrays

by fruiture (Curate)
on Mar 07, 2003 at 17:15 UTC ( #241189=note: print w/replies, xml ) Need Help??


in reply to arrays from arrays

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

Replies are listed 'Best First'.
Re: Re: arrays from arrays
by Anonymous Monk on Mar 07, 2003 at 17:50 UTC

    Excellent, thanks for that, worked perfectly, even if I don't quite know how... will real up on Date::Dumper on Monday..

    Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://241189]
help
Chatterbox?
choroba fears Amsterdam might fill him with enthusiasm to do nothing
[robby_dobby]: Uff, too expensive :-(
[LanX]: ... I found YAPC EU more entertaining (and international )than YAPC NA
[robby_dobby]: What's the site for YAPC::EU?
[LanX]: Robby Dobby no IIRC Sinai is already part of Asia
[robby_dobby]: blogs.perl.org page for yapceu is out of date by 2 years
[robby_dobby]: LanX: Sharm-el-Sheikh is in Egypt
[choroba]: act.yapc.eu
[robby_dobby]: Thanks, choroba
[LanX]: yes and Egypt (like Turkey and Russia) belongs to two continents

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-04-24 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (442 votes). Check out past polls.