sub pretty_table { # prettyTable($aString, @aList); @aList = ( [...], [...] ); # by i_am_jojo@msn.com my ($title, @data) = @_; my @temp; my @max_length; my $row_length; my $indent = 4; my $the_table; foreach my $col (0..$#{$data[0]}) { push @{$temp[$col]}, $_->[$col +] foreach (@data); } $max_length[$_] = length( (sort{length($b) <=> length($a)} @{$data +[$_]} )[0]) + 2 foreach (0..$#data); $row_length+= $max_length[$_] foreach (0..$#{$temp[0]}); $row_length+= $#data; $the_table = ' ' x $indent.'+'.'-' x $row_length."+\n"; $the_table.= ' ' x $indent.'| '.$title.' ' x ($row_length - length +($title) - 1)."|\n"; foreach my $row (0..$#temp) { $the_table.= ' ' x $indent; $the_table.= '+'.'-' x $max_length[$_] foreach (0.. $#{$temp[0 +]}); $the_table.= "+\n"; $the_table.= ' ' x $indent; $the_table.= '| '.@{$temp[$row]}[$_].' ' x ($max_length[$_] - +length(@{$temp[$row]}[$_]) - 1) foreach (0.. $#{$temp[0]}); $the_table.= "|\n"; } $the_table.= ' ' x $indent; $the_table.= '+'.'-' x $max_length[$_] foreach (0.. $#{$temp[0]}); $the_table.= "+\n"; return $the_table; } #==Output eXample== # +-------------------------------------------------+ # | IP Header | # +--------+---------------+---------+--------------+ # | ver | 4 | hlen | 5 | # +--------+---------------+---------+--------------+ # | tos | 0 | len | 60 | # +--------+---------------+---------+--------------+ # | flags | 2 | foffset | 0 | # +--------+---------------+---------+--------------+ # | id | 16649 | ttl | 64 | # +--------+---------------+---------+--------------+ # | src_ip | xx.xx.xx.xx | dest_ip | xx.xx.xx.xx | # +--------+---------------+---------+--------------+ # | proto | 6 | cksum | 44477 | # +--------+---------------+---------+--------------+ # +-----------------------------------------+ # | TCP Header | # +----------+-----------+-----------+------+ # | src_port | 32850 | dest_port | 80 | # +----------+-----------+-----------+------+ # | seqnum | 976483812 | acknum | 0 | # +----------+-----------+-----------+------+ # | hlen | 10 | reserved | 0 | # +----------+-----------+-----------+------+ # | flags | 2 | winsize | 5808 | # +----------+-----------+-----------+------+ #==End==
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: pretty_table()
by merlyn (Sage) on May 03, 2005 at 10:10 UTC | |
by rafl (Friar) on May 31, 2005 at 13:21 UTC |