Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Convert hash into an array based matrix

by madM (Beadle)
on Aug 13, 2013 at 13:52 UTC ( [id://1049275]=perlquestion: print w/replies, xml ) Need Help??

madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! i have a hash of hashes and i want to create with the information an Array of array for example:
%HoH = ( A => { A => "1", B => "4", C => "2", }, B => { A => "4", B => "10", C => "3", }, C => { A => "2", B => "3", C => "9", }, );
and i want to create a 3x3 @array of arrays like this:
@array=( [1, 4, 2], [4, 10, 3], [2, 3, 9]);
does anybody has any suggestions? Thanks!

Replies are listed 'Best First'.
Re: Convert hash into an array based matrix
by choroba (Cardinal) on Aug 13, 2013 at 14:03 UTC
    Loops provided a solution where values are not sorted in the inner arrays. I was just about to post a similar solution:
    my @array = map [ values %{ $HoH{$_} } ], sort keys %HoH;
    If you want to sort them byt he keys, you can use a bit more complicated incantation:
    my @array = map { my $k = $_; [ map $HoH{$k}{$_}, sort keys %{ $HoH{$_} } ] } sort keys %HoH;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Yes, a more complete solution. Was just about to update my post with a solution like yours. ;o)
Re: Convert hash into an array based matrix
by NetWallah (Canon) on Aug 13, 2013 at 14:08 UTC
    This one maintains 2 levels of Key order:
    my @a=map { my $x=$_;[map { $HoH{$x}{$_} } sort keys %{$HoH{$x}} ] } + sort keys %HoH;
    Update: Just noticed that choroba beat me to the same solution.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Convert hash into an array based matrix
by Loops (Curate) on Aug 13, 2013 at 13:58 UTC
    use Data::Dump qw(dd); my %HoH = ( A => { A => "1", B => "4", C => "2", }, B => { A => "4", B => "10", C => "3", }, C => { A => "2", B => "3", C => "9", }, ); my @a; push @a, [ values $HoH{$_} ] for sort keys %HoH; dd @a;
    Just iterate through each key, and accumulate the values into your array. The above prints:
    ([1, 2, 4], [4, 3, 10], [2, 9, 3])
Re: Convert hash into an array based matrix
by AnomalousMonk (Archbishop) on Aug 13, 2013 at 20:28 UTC

    Uses arbitrary order, which could be sorted, or even two different orders:

    >perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; my %HoH = ( A => { A => '1', B => '4', C => '2', }, B => { A => '4', B => '10', C => '3', }, C => { A => '2', B => '3', C => '9', }, ); ;; my @order1 = my @order2 = ('A' .. 'C'); ;; my @ra = map [ @{ $HoH{$_} }{ @order1 } ], @order2; ;; my @array = ( [1, 4, 2], [4, 10, 3], [2, 3, 9]); ;; is_deeply \@ra, \@array; " ok 1 ok 2 - no warnings 1..2

    Update: Changed example code to show use of separate orders.

Re: Convert hash into an array based matrix
by Anonymous Monk on Aug 13, 2013 at 14:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1049275]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found