http://qs321.pair.com?node_id=676111

andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

With Text::Table, I wonder if it's possible to add horizontal lines (rules) between each cell while not splitting multi-line cells.
$ cat ~/test/perl/text.table/text.table.pl use strict; use warnings; use Text::Table; my $tb = Text::Table->new(\'| ', "", \' | ', "Field ", \'| ', "Length ", \'| +', "Comment ", \' |'); my @AoA = ( [ 1, "Foo", "20", "Foo" ], [ 2, "Bar", "35", "Bar\nBar" ], [ 3, "Tze", "10", "Tze\nTze" ], ); $tb->load(@AoA); my $rule = $tb->rule(qw/- +/); my @arr = $tb->body; print $rule, $tb->title, $rule; for (@arr) { print $_ . $rule; } __END__
Actual output:
$ /usr/bin/perl ~/test/perl/text.table/text.table.pl +---+-------+--------+----------+ | | Field | Length | Comment | +---+-------+--------+----------+ | 1 | Foo | 20 | Foo | +---+-------+--------+----------+ | 2 | Bar | 35 | Bar | +---+-------+--------+----------+ | | | | Bar | +---+-------+--------+----------+ | 3 | Tze | 10 | Tze | +---+-------+--------+----------+ | | | | Tze | +---+-------+--------+----------+
Wanted output:
$ /usr/bin/perl ~/test/perl/text.table/text.table.pl +---+-------+--------+----------+ | | Field | Length | Comment | +---+-------+--------+----------+ | 1 | Foo | 20 | Foo | +---+-------+--------+----------+ | 2 | Bar | 35 | Bar | | | | | Bar | +---+-------+--------+----------+ | 3 | Tze | 10 | Tze | | | | | Tze | +---+-------+--------+----------+
--
Andreas

Replies are listed 'Best First'.
Re: Text::Table - horizontal lines and multiline cells
by moklevat (Priest) on Mar 25, 2008 at 13:32 UTC

    Hi Andreas,

    Is it absolutely necessary to use Text::Table? I really like Perl6::Form for this kind of thing. Here is a quick (recycled) example:

    #!/usr/bin/perl -w use strict; use Perl6::Form; my @barcodes=qw/8129348 2354234 234234534 21342134 23453421/; my @prices = qw/625.50 626.35 234.23 546.54 3245.45/; my @quants = qw/2 2 34 4 6/; my @values = qw/21345 345345 345345 3453 34534/; my $total = 0; foreach(@values){$total+=$_}; print form ' ============================================================', '| Goods in Stock |', '|===========================================================|', '| Barcode | Price | Quantity | Value |', '|-------------+---------------+-------------+---------------|', '| {[[[[[[[[[} | {$]]]]]].[[[} | {]]]]]]]]]} | {$]]]]]]].[[[}|', \@barcodes, \@prices, \@quants, \@values, '|===========================================================|', '| Total Value In Stock {$]]]]]]].[[[}|', $total, ' =========================================================== ', ;

    Produces:

    =========================================================== | Goods in Stock | |===========================================================| | Barcode | Price | Quantity | Value | |-------------+---------------+-------------+---------------| | 8129348 | $625.5 | 2 | $21345.0 | | 2354234 | $626.35 | 2 | $345345.0 | | 234234534 | $234.23 | 34 | $345345.0 | | 21342134 | $546.54 | 4 | $3453.0 | | 23453421 | $3245.45 | 6 | $34534.0 | |===========================================================| | Total Value In Stock $750022.0 | ===========================================================
Re: Text::Table - horizontal lines and multiline cells
by Thelonius (Priest) on Mar 25, 2008 at 14:05 UTC
    I haven't tested this, but looking at the code for Text::Table, it looks like this should work:
    { local ($/) = "\xff\xff"; # some string not in your data; $tb->load(@AoA); }
    'cuz it's splitting on $/