Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Generating Hash-of-List using map?

by LanX (Canon)
on Sep 23, 2013 at 17:06 UTC ( #1055334=note: print w/ replies, xml ) Need Help??


in reply to Generating Hash-of-List using map?

probably not an expected answer:

DB<117> %hash=() DB<118> map { ($k,$e)=split '-'; push @{ $hash{$k} },$e } @CELLS => (1, 2, 3, 4, 1, 2, 1, 2) DB<119> \%hash => { A => [1, 2, 3, 4], B => [5, 6], C => [7, 8] }

update

or

DB<125> %hash=() DB<126> push @{ $hash{$_->[0]} } , $_->[1] for map {[split '-' ]} @ +CELLS => "" DB<127> \%hash => { A => [1, 2, 3, 4], B => [5, 6], C => [7, 8] }

or

DB<128> %hash=() DB<129> map { push @{ $hash{$_->[0]} } , $_->[1] } map {[split '-' +]} @CELLS => (1, 2, 3, 4, 1, 2, 1, 2) DB<130> \%hash => { A => [1, 2, 3, 4], B => [5, 6], C => [7, 8] }

or ( but not recommended b/c of possible side effects)

DB<135> %hash=() DB<136> map { @_=split '-'; push @{$hash{$_[0]}},$_[1] } @CELLS => (1, 2, 3, 4, 1, 2, 1, 2) DB<137> \%hash => { A => [1, 2, 3, 4], B => [5, 6], C => [7, 8] }

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: Generating Hash-of-List using map?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055334]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-09-16 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (45 votes), past polls