I write this func to dump packet headers in my sniffer script.
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==

In reply to pretty_table() by shanleiguang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.