Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to printf a matrix like this

by madM (Beadle)
on Feb 11, 2014 at 10:48 UTC ( #1074381=perlquestion: print w/ replies, xml ) Need Help??
madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! Im trying to print a 4 x 4 matrix but i dont really know how to round the numbers and print them at the same time with printf with a spacing of ,for example, 4 Heres what i wrote: the numbers are stored in a hash of hashes with the keys A B C and D
A => A 6.8888 B => B 6.8888 C => C 6.8888 D => D 6.8888
the rest of the numbers are 0.1111
my @letters=qw(A B C D); print ' '; foreach my $key (@letters){ printf ("%s", $key); } "\n"; foreach my $key (@letters) { print $key,' '; foreach my $key2 (@letters) { printf ("%.1f" , $A->{$key}->{$key2}); } print "\n"; } }
Heres what im trying to get:
A B C D A 6.8 0.1 0.1 0.1 B 0.1 6.8 0.1 0.1 C 0.1 0.1 6.8 0.1 D 0.1 0.1 0.1 6.8

Comment on how to printf a matrix like this
Select or Download Code
Re: how to printf a matrix like this
by McA (Priest) on Feb 11, 2014 at 11:09 UTC

    Hi,

    IMHO the most generic approach would be to store the result matrix into an array of arrays. While doing so, you could remember the most needed space for a column. After that you know the minimum space for each column and the adjustment needed when printing. After that you print each row of the array of arrays knowing how to adjust the length of the output.

    Regards
    McA

Re: how to printf a matrix like this
by Anonymous Monk on Feb 11, 2014 at 11:11 UTC
      im asking again because im not getting a good answer for my questions

        im asking again because im not getting a good answer for my questions

        what does that mean?

Re: how to printf a matrix like this
by RichardK (Priest) on Feb 11, 2014 at 11:46 UTC

    The explanation of how to use printf formatting is in the help for sprintf. (the printf help really should point to this too).

    To fix your problem you need to set a minimum width, try something like this :-

    printf "%6.1f",$var;
      thakyou! that really helped :)
      "The explanation of how to use printf formatting is in the help for sprintf. (the printf help really should point to this too)." [my emphasis]

      The printf documentation does point to this. It starts with:

      "Equivalent to print FILEHANDLE sprintf(FORMAT, LIST), ..."

      [FWIW, madM has already been provided with that information (in Re: printf matrix) as well as example sprintf usage in a number of places.]

      -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-12-22 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (114 votes), past polls