Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Allignment of Chars

by ganilmohan (Acolyte)
on Aug 07, 2008 at 05:36 UTC ( #702807=perlquestion: print w/ replies, xml ) Need Help??
ganilmohan has asked for the wisdom of the Perl Monks concerning the following question:

My Results shows
Jon Stuart Robin    20
Alec 40
Sweety Mc Stewart Roman 420
Bony 230
I Used the following statement "print "$name\t$Value\n";". How can I allign my results? But i wanted as
Jon Stuart Robin              20
Alec 40
Sweety Mc Stewart Roman 10
Bony 230

Comment on Allignment of Chars
Re: Allignment of Chars
by prasadbabu (Prior) on Aug 07, 2008 at 05:42 UTC
Re: Allignment of Chars
by pc88mxer (Vicar) on Aug 07, 2008 at 05:47 UTC
Re: Alignment of characters
by parv (Priest) on Aug 07, 2008 at 06:09 UTC
    use warnings; use strict; use List::Util qw[ max ]; my %p = ( 'a' => 1 , 'm l n o p q' => 20 , 'x y z' => 321 ); my $max = max map { length $_ } keys %p; my $format = "%-${max}s %d\n\n"; my $out = ''; for ( sort keys %p ) { $out .= sprintf $format , $_ , $p{ $_ } } print $out;

    Main thing is to find out the maximum length of each column that may affect the format (alignment if you like), to be used in sprintf format specification. In case numbers should be aligned with right margin ...

    use warnings; use strict; use List::Util qw[ max ]; my %p = ( 'a' => 1 , 'm l n o p q' => 20 , 'x y z' => 321 ); my $name = max map { length $_ } keys %p; my $number = max map { length $_ } values %p; my $format = "%-${name}s %${number}d\n\n"; my $out = ''; for ( sort keys %p ) { $out .= sprintf $format , $_ , $p{ $_ } } print $out;
Re: Allignment of Chars
by eosbuddy (Scribe) on Aug 07, 2008 at 07:14 UTC

Log In?
Username:
Password:

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

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

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





    Results (119 votes), past polls