Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 rifling through the Monastery: (15)
As of 2015-07-06 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (82 votes), past polls