Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

structured printing of arrays

by hacker_j99 (Beadle)
on Apr 24, 2003 at 02:36 UTC ( [id://252747]=perlquestion: print w/replies, xml ) Need Help??

hacker_j99 has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

I have a array of arrays.
how can i get it to look like this with values of unknown length
+----+----------+------------+-----------+---------+----------+ | id | user_lvl | first_name | last_name | surname | homepage | +----+----------+------------+-----------+---------+----------+ | 1 | 50 | unknownlen | | | none | +----+----------+------------+-----------+---------+----------+

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: structured printing of arrays
by graff (Chancellor) on Apr 24, 2003 at 03:55 UTC
    There is a handy module on CPAN called Data::ShowTable, which provides the pretty borders and handles a lot of the busywork of formatting the table output, but using it effectively for this sort of thing still involves some work -- you have to tell it what the maximum column widths are that you want for your table (and if you hand it data that doesn't fit within those widths, it will do an adequate, readable, but not-so-pretty job if wrapping the contents within a table cell, so no information is lost). Here's a relevant sample of usage -- I never used this module before, and I figured out the example based on reading the man page (so the documentation for this module is good):
    use Data::ShowTable; my @AoA = ( [ qw/id user_lvl first_name last_name surname homepage/ ], [ '1', '50', 'unknownlengh', '', ' ', 'none' ], [ qw/5000 0 Joe Papadakolopolous AlsoTooLong www.younameit.org/ ], ); my $header = shift @AoA; # extract the column labels to a separate ar +ray # You can set "default" column widths to be the lengths of # the column header strings, like this: my @colwidth = map { length() } @$header; # If you don't do at least that much work to initialize # column widths, you probably won't like the output. # figure out the maximum data widths for each column; these will have +an # effect on output when they happen to be wider than the corresponding # column header string: my @colwidth; for $row ( @AoA ) { for ( $col=0; $col<@$row; $col++ ) { my $l = length( $$row[$col] ); $colwidth[$col] = $l if ( $colwidth[$col] < $l ); } } # Note that the args this and other ShowTable methods are all REFERENC +ES: ShowBoxTable $header, [qw/int int text text text text/], \@colwidth, \ +&rowsub; # Here's the sub that ShowTable needs so that it can fetch # one row of data per call: sub rowsub { my $arg = shift; if ( $arg ) { return; } elsif ( scalar @AoA ) { my $rowref = shift @AoA; return (@$rowref); } else { return; } }
Re: structured printing of arrays
by jasonk (Parson) on Apr 24, 2003 at 03:11 UTC
      Perfect! Text::ASCIITable works great and does most things for me. In either ascii or in html table format...
      #!/eng/local/bin/perl # table maker require '/home/top/hfranks/perl/ASCIITable.pm'; # database hack require "../mysql_handeler.pl"; require "../template.pl"; #new table $t = new Text::ASCIITable; $user="hfranks"; @id=mysql_handeler('select uid from users_student where name="'.$user. +'";'); $uid=$id[1]; #mysql statements @res=mysql_handeler('select * from EECS2550 where uid="'.$uid.'";'); chomp @res; #column title @top=split(/ /, shift(@res)); #remove 2 unwanted auto_increment fields shift (@top); shift (@top); $t->setCols(\@top); #get rows foreach $line (@res){ chomp $line; @ary = split(/ /, $line); #remove two unwanted auto_increment fields shift (@ary); shift (@ary); $t->addRow(@ary); } #print it #normal ascii output #$cOntent .= $t->draw(); #advanced html output $cOntent.=$t->draw( ['<table border="1">','','',''], # <table><tr></tr> ['<tr><td>','</td></tr>','</td><td>'], ['','','',''], ['<tr><td>','</td></tr>','</td><td>'], ['','</table>','',''], ); my $template = "../template.html"; my %gTokens=(); my $key="<!--something-->"; $gTokens{"$key"} = $cOntent; &SendTemplate($template,%gTokens);
      html result:

      test1 test2 test3 quiz1 homework final
      100 100 100 10 100 100

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found