Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Creating hash of arrays (in a faster way)

by LanX (Canon)
on Nov 01, 2013 at 13:21 UTC ( #1060761=note: print w/ replies, xml ) Need Help??


in reply to Creating hash of arrays (in a faster way)

looks for me like you want to transpose a matrix with headlines (aka table) to get hashes of columns.

just for fun a solution with List::MoreUtils :

(though I doubt it's faster...as long you are not slurping a text table =)

DB<201> \@tab => [ ["A", "B", "C", "D"], ["a1", "b1", "c1", "d1"], ["a2", "b2", "c2", "d2"], ["a3", "b3", "c3", "d3"], ] DB<202> @head= @{$tab[0]} => ("A", "B", "C", "D") DB<203> use List::MoreUtils qw/part/ DB<204> $i=-1; %col=(); DB<205> @col{@head} = part {$i++; $i %= @head } map {@$_} @tab[1..3] + => ( ["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"], ["d1", "d2", "d3"], ) DB<206> \%col => { A => ["a1", "a2", "a3"], B => ["b1", "b2", "b3"], C => ["c1", "c2", "c3"], D => ["d1", "d2", "d3"], }

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: Creating hash of arrays (in a faster way)
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-18 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (105 votes), past polls