Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Column wise entry in text table

by reaper9187 (Scribe)
on Jan 20, 2013 at 15:32 UTC ( #1014295=perlquestion: print w/ replies, xml ) Need Help??
reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks ..
back with another query .. I'm trying to format the result of my application in a text format using Tk.. Its actually a summary report of time vs performance stats.I'm currently using Text::Table. I find it very easy to use. there is a limitation on the data entry though.. it is row-wise by default. I wanted to know if there is a way to enter the day column wise using Text::table... Sample code
use Text::Table; my $tb = Text::Table->new( "Planet", "Radius\nkm", "Density\ng/cm^3" ); $tb->load( [ "Mercury", 2360, 3.7 ], [ "Venus", 6110, 5.1 ], [ "Earth", 6378, 5.52 ], [ "Jupiter", 71030, 1.3 ], ); print $tb;
so in above case, i want to enter column 1 first then column 2 nd so on .
Also, the number of columns is variable . Please help

Comment on Column wise entry in text table
Download Code
Re: Column wise entry in text table
by Cristoforo (Deacon) on Jan 20, 2013 at 18:18 UTC
    The code you provided is just the sample code for Text::Table. What have you tried?

    You will need to transpose the table before you submit it to Text::Table. Something like this: my @transpose = transpose(@matrix); where @matrix is your array to transpose.

    sub transpose { my @array = @_; my $max_j = max map $#$_, @array; my @trans; for my $i (0 .. $#array) { for my $j (0 .. $max_j) { $trans[$j][$i] = $array[$i][$j]; } } return @trans; }
    This requires the function 'max' from List::Util.

    Update: The transpose function above will not enter '0' in the array if it is in the data. It should be correct now.

    I'm not sure whether it is right to set a max_j as it is so, here it is without that detail.

    sub transpose { my @array = @_; my @trans; for my $i (0 .. $#array) { for my $j (0 .. $#{$array[$i]}) { $trans[$j][$i] = $array[$i][$j]; } } return @trans; }
Re: Column wise entry in text table
by mhearse (Hermit) on Jan 21, 2013 at 20:36 UTC
    For text reports, I use either column or w3m to render my reports. w3m is nice because it can render/dump html.
    cat <<EOT | column -s : -t > Numeric:Value > 1:one > 2:two > 3:three > 4:four > 5:five > EOT Numeric Value 1 one 2 two 3 three 4 four 5 five
      A w3m example:
      cat <<EOT | w3m -T text/html -dump > <html> > <table border=1> > <tr> > <th>Numeric</th> > <th>Value</th> > </tr> > <tr> > <td>1</td> > <td>one</td> > </tr> > <tr> > <td>2</td> > <td>two</td> > </tr> > <tr> > <td>3</td> > <td>three</td> > </tr> > </table> > </html> > EOT
      │Numeric │Value│
      │1       │one  │
      │2       │two  │
      │3       │three│

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-10-23 15:12 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (125 votes), past polls