Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Allignment of Chars

by ganilmohan (Acolyte)
on Aug 07, 2008 at 05:36 UTC ( [id://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

Replies are listed 'Best First'.
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: Allignment of Chars
by eosbuddy (Scribe) on Aug 07, 2008 at 07:14 UTC
Re: Alignment of characters
by parv (Parson) 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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found