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

Hi Monks! I´m trying to print a matrix with this kind of format:
* = minimum score of the column
A R N D * A 4 -1 -2 -2 -2 R -1 5 0 -2 -2 N -2 0 6 1 -2 D -2 -2 1 6 -2 * -2 -2 -2 -2 4 my data structure sees like this: $VAR1 = { 'A' => { 'A' => 4, 'R' => -1, 'N' => -2, 'D' => -2, }, 'R' => {'A'=> -1, 'R'=> 5, and so on with all rows
I wrote this so far:
my @letters=qw( A R N D); sub printMatrix1 { my ($A) = @_; foreach my $key (sort keys %$A) { print $key." "x2; foreach my $key2 (sort keys %{$A->{$key} } ) { printf( "%.4f " , $A->{$key}->{$key2} ); } print "\n"x2; } }
But i don´t know how to print the letters above A R N D in that specific order and exactly above the numbers regardless of how many digits the numbers are. Any suggestions would be really apreciated.. thanks!

Replies are listed 'Best First'.
Re: printing Matrix
by karlgoethebier (Abbot) on Jan 07, 2014 at 09:44 UTC
Re: printing Matrix
by hdb (Monsignor) on Jan 07, 2014 at 10:02 UTC

    Try to replace sort keys %$A with @letters and see what happens. If you want to print a string with a given width you could try

    printf "%4s ", $key;