Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Text::Table w/ newlines

by diotalevi (Canon)
on Sep 03, 2004 at 20:32 UTC ( [id://388397]=perlquestion: print w/replies, xml ) Need Help??

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

[Text::Table has handled newlines for a few years now. This node is out of date.]

Text::Table doesn't handle newlines. What is the expected work around for Text::Table? Is there another table formatting module that handles this?

my $t = Text::Table->( qw( ID Name Source Type Assigment ) ); $t->load( [ 1, '...', '...', '...', '...' ], [ 2, '...', "NA\nEU", "Emp\nEx-emp", "Active\nInactive" ], [ 3, '...', '...', '...', '...' ] ); print $t;

This is what is produced.

ID Name Source Type Assignment 1 ... ... ... ... 2 ... NA EU Emp Ex-emp Active Inactive 3 ... ... ... ...

This is what should be produced.

ID Name Source Type Assignment 1 ... ... ... ... 2 ... NA Emp Active EU Ex-emp Inactive 3 ... ... ... ...

Replies are listed 'Best First'.
Re: Text::Table w/ newlines
by tye (Sage) on Sep 03, 2004 at 20:51 UTC

    Use Algorithm::Loops:

    #!/usr/bin/perl -w use strict; require Text::Table; use Algorithm::Loops qw( MapCarU ); my $t = Text::Table->new( qw( ID Name Source Type Assigment ) ); my @rows= ( [ 1, '...', '...', '...', '...' ], [ 2, '...', "NA\nEU", "Emp\nEx-emp", "Active\nInactive" ], [ 3, '...', '...', '...', '...' ], ); $t->load( map { MapCarU { [ map { defined($_) ? $_ : '' } @_ ] } map { [ split $/ ], } @$_ } @rows, ); print $t;

    produces

    ID Name Source Type Assigment 1 ... ... ... ... 2 ... NA Emp Active EU Ex-emp Inactive 3 ... ... ... ...

    Update: Based on blokhead's observation, the heart of the code can be made quite concise:

    $t->load( map { MapCarU {[@_]} map [split $/], @$_ } @rows );

    Update2: Replaced erroneous use of Filter with map.

    - tye        

      There's a diff to add this feature to Text::Table at http://grenekatz.org/downloads/perl/Text-Table-1.103.diff complete with extensions to the tests


      Values with newlines in them are split to the next line properly. I also added the $tb object as the return value of ->add and ->load so that method calls could be chained.

      print Text::Table ->new( qw( ID Name Source Type Assigment ) ) ->load( [ 1, '...', '...', '...', '...' ], [ 2, '...', "NA\nEU", "Emp\nEx-emp", "Active\nInactive" +], [ 3, '...', '...', '...', '...' ] ); ID Name Source Type Assignment 1 ... ... ... ... 2 ... NA Emp Active EU Ex-emp Inactive 3 ... ... ... ...
Re: Text::Table w/ newlines
by blokhead (Monsignor) on Sep 03, 2004 at 20:58 UTC
    tye's mapcar solution is pretty elegant. Here's what I came up with to preprocess the list of rows before sending to Text::Table:
    my @lines = ( [ 1, '...', '...', '...', '...' ], [ 2, '...', "NA\nEU\nThird line", "Emp\nEx-emp", "Active\nInactive" +], [ 3, '...', '...', '...', '...' ] ); for (my $l = 0; $l < @lines; $l++) { next unless grep /\n/, @{ $lines[$l] }; splice @lines, $l+1, 0, []; ($lines[$l][$_], $lines[$l+1][$_]) = split /\n/, $lines[$l][$_], 2 for 0 .. $#{ $lines[$l] }; } use Data::Dumper; print Dumper \@lines;
    (A good example of why sometimes a C-style for loop is needed) Looks like Text::Table converts undef entries to empty string, so you don't need to do anything special to the undefs after the split.

    blokhead

Re: Text::Table w/ newlines
by gmpassos (Priest) on Sep 03, 2004 at 21:12 UTC
    Take a look at Perl6::Form.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: Text::Table w/ newlines
by Anonymous Monk on Sep 03, 2004 at 22:44 UTC
    use Text::ASCIITable;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found