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

Hi All,

First of all I would like to thank all for you for your efforts and time.

I have formed a hash and I am trying to for a table from the hash

contents.

which i am able to do, but I am not able to align the text in

the columns

to center. They are aligned to left which is the default.

My code

$analysis_table = Text::Table->new(\'| ',"VIEW_NAME",\' | ',"UNCERTAIN +ITY_TYPE\n TNS | WNS ",\' | '); foreach my $key(keys %analysis_hash){ my $temp = "$analysis_hash{$key}->{'uncertainity_type'} | $ana +lysis_hash{$key}->{'uncertainity_type'}" ; $analysis_table->add($key,$temp) ; } my $rule = $analysis_table->rule(qw/- +/) ; my @body = $analysis_table->body; print $rule, $analysis_table->title, $rule; for (@body) { # align('center',$_); print $_ . $rule ; }

My Output

+-----------+-------------------+

|VIEW_NAME |UNCERTAINITY_TYPE |

+-----------+-------------------+

|view1 |0.1 | 0.2 |

+-----------+-------------------+

|view2 |0.3 | 0.4 |

+-----------+-------------------+

Desired output

+-----------+-------------------+

| VIEW_NAME | UNCERTAINITY_TYPE |

+-----------+-------------------+

| view1 | 0.1 | 0.2 |

+-----------+-------------------+

| view2 | 0.3 | 0.4 |

+-----------+-------------------+

every thing aligned to center. I tried using Text::Aligner but to no success.

Please help.

Replies are listed 'Best First'.
Re: alignment of text in Text::Table
by Cristoforo (Curate) on Aug 19, 2012 at 17:13 UTC
    Here is a small program that shows how to align the data. You should be able to run it.

    Please follow 2teez's advise and look at the link he provided to see how to format the code/data in your post. (You can edit your post after you first post it).

    #!/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('-','-'); __END__ Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\perlp\Text_Table>perl try.pl | Planet | Radius | Density | |---------|--------|---------| | Mercury | 2360 | 3.7 | | Jupiter | 71030 | 1.3 | ------------------------------ C:\perlp\Text_Table>
    Note: This example was posted by Marto in this node.

      Thanks, Cristoforo for your time and effort. Sorry for not formating. It's my first post and I am not well versed. I have corrected the post as you and 2teez pointed. Following your advice I got what I wanted. Thanks and cheers

      kramer

Re: alignment of text in Text::Table
by 2teez (Vicar) on Aug 19, 2012 at 11:29 UTC