Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by LanX (Chancellor)
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 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)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060761]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-07-22 18:13 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.