perl - How can I print a table with multi-line strings using the Text::Table module? -


i using cpan text::table module. have table in script , values multi-line strings. using rule print table.

my code follows:

#!/usr/bin/perl5.14.1 use findbin; use lib "$findbin::bin/mylib/text-aligner-0.12/lib/"; use lib "$findbin::bin/mylib/text-table-1.130/lib/"; use text::table;  $tb = text::table->new(\'| ', "", \' | ', "field ", \'| ', "length ", \'| ', "comment ", \' |'); @aoa = (   [ 1, "foo", "20", "foo" ],   [ 2, "bar", "35", "bar\nbar" ],   [ 3, "tze", "10", "tze\ntze" ], );  $tb->load(@aoa); $rule = $tb->rule(qw/- /); @arr = $tb->body;   print $rule, $tb->title, $rule; (@arr) {   print $_ . $rule; } 

however when run following:

|---|-------|--------|----------| |   | field | length | comment  | |---|-------|--------|----------| | 1 | foo   | 20     | foo      | |---|-------|--------|----------| | 2 | bar   | 35     | bar      | |---|-------|--------|----------| |   |       |        | bar      | |---|-------|--------|----------| | 3 | tze   | 10     | tze      | |---|-------|--------|----------| |   |       |        | tze      | |---|-------|--------|----------| 

is there way not print separate lines in case of multi-line strings?

i want display table follows:

|---|-------|--------|----------| |   | field | length | comment  | |---|-------|--------|----------| | 1 | foo   | 20     | foo      | |---|-------|--------|----------| | 2 | bar   | 35     | bar      | |   |       |        | bar      | |---|-------|--------|----------| | 3 | tze   | 10     | tze      | |   |       |        | tze      | |---|-------|--------|----------| 

the text::table code calls split( /\n/ ) on each row of data , puts results own rows. can work around calculating rows correspond multi-line data , printing rule @ appropriate boundaries:

use strict; use warnings;  use list::util qw(max); use text::table;  $sep = \'|'; @col_spec = ($sep, '', $sep, 'field', $sep, 'length', $sep, 'comment', $sep);  $tb = text::table->new(@col_spec);  @data = (     [ 1, "foo", "20", "foo" ],     [ 2, "bar\nbaz\nqux", "35", "bar\nbar" ],     [ 3, "tze", "10", "tze\ntze" ] );  # track rows should preceded rules. key of 'n' indicates # rule should printed before nth row (zero-indexed). %indexes = (     0 => 1, # before header row     1 => 1  # after header row );  # calculate body rows rules should printed $count = 1; foreach $row (@data) {     # greatest number of newlines in row     $newlines = max map { tr/\n// } @$row;      # 1 newline corresponds 2 rows     $count += $newlines + 1;      $indexes{$count} = 1; }  $tb->load(@data);  $rule = $tb->rule('-', '+');  foreach $i (0 .. $tb->height) {     print $rule if exists $indexes{$i};     print $tb->table($i); } 

output:

+-+-----+------+-------+ | |field|length|comment| +-+-----+------+-------+ |1|foo  |20    |foo    | +-+-----+------+-------+ |2|bar  |35    |bar    | | |baz  |      |bar    | | |qux  |      |       | +-+-----+------+-------+ |3|tze  |10    |tze    | | |     |      |tze    | +-+-----+------+-------+ 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -