Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Printing Issues with Text::Table

by neversaint (Deacon)
on Nov 23, 2005 at 09:43 UTC ( [id://511036]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
How can I modify this code below
use Text::Table; my $tb = Text::Table->new("Planet", "Radius", "Density"); $tb->load( [ "Mercury", 2360, 3.7 ], [ "Jupiter", 71030, 1.3 ], ); print $tb;
such that it prints with:
  • separator and tabbed space
  • centered title
  • left justified content

Something like:
| Planet | Radius | Density | <-- Center the title |----------------------------------- | |Mercury | 2360 | 3.7 | <-- Left Justify |Jupiter | 71030 | 1.3 | also tabbed with separator --------------------------------------
Update: I asked this because I couldn`t make sense of its sparse documentation. Please bear with me...

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Printing Issues with Text::Table
by marto (Cardinal) on Nov 23, 2005 at 11:34 UTC
    neversaint,

    A quick example to get you started based on the sample data you have given:
    #!/usr/bin/perl use strict; use warnings; use Text::Table; my $tb = Text::Table->new( \'| ', { title => 'Planet', align => 'center', align_title => 'center' }, \" | ", { title => 'Radius', align => 'center', align_title => 'center' }, \" | ", { title => 'Density', align => 'center', align_title => 'center' }, \' |', ); $tb->warnings('on'); $tb->load( [ "Mercury", 2360, "3.7" ], [ "Jupiter", 71030, "1.3" ], ); print $tb->title(); print $tb->rule('-','|'); print $tb->body(0); print $tb->body(1); print $tb->body_rule('-','-');
    Which produces:
    | Planet | Radius | Density | |---------|--------|---------| | Mercury | 2360 | 3.7 | | Jupiter | 71030 | 1.3 | ------------------------------
    Hope this helps,

    Martin
Re: Printing Issues with Text::Table
by Moron (Curate) on Nov 24, 2005 at 13:47 UTC
    Mixing tabs and spaces has always been a recipé for disaster, but if you are prepared to settle for spaces throughout, then the simplest technique is to use a format code processor (here I used LEFT CENTRE and LM4 for the more weird one) which requires only a tiny piece of code each when applied with the minor trick of overwriting a preformatted template field of just spaces.
    #!/usr/bin/perl # (tested) use strict; use warnings; my @AoA = (); Insert( \@AoA, 'Planet', 'Radius', 'Density' ); Insert( \@AoA, 'Mercury', 2360, 3.7 ); Insert( \@AoA, 'Jupiter', 71030, 1.3 ); print Formit( \@AoA ); sub Formit { my $aoaref = shift; my $aref = $aoaref -> [0]; my $return = Line($aref); $return .= FormatLine($aref, 'CENTRE', 'CENTRE', 'CENTRE' ); $return .= Line($aref); shift @$aoaref; for $aref ( @$aoaref ) { $return .= FormatLine( $aref, 'LEFT', 'LM4', 'LM4' ); } $return .= Line( $aref ); return $return; } sub FormatLine{ my $aref = shift; my $return = '|'; for my $field ( @$aref ) { my $format = shift; my $formatted = ( ' ' x 12 ); my $pos = ( $format eq 'LEFT' ) ? 0 : ( $format eq 'CENTRE' ) ? 6 - (length( $field ) / 2 +) : ( $format =~ /^LM(\d+)/ ) ? $1 : 0; substr( $formatted, $pos, length( $field ) ) = $field; $return .= $formatted . '|'; } return $return . "\n"; } sub Line { my $aref = shift; my $cols = ( 13 * ( $#$aref + 1 ) ) + 1; return ( '-' x $cols ) . "\n"; } sub Insert{ my $aoaref = shift; my @a = @_; push @$aoaref, \@a; }

    -M

    Free your mind

Re: Printing Issues with Text::Table
by Anonymous Monk on Nov 23, 2005 at 16:39 UTC

Log In?
Username:
Password:

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

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

    No recent polls found