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

Hello Monks! I am new to Perl and I was wondering if anyone could lead me in the right direction for formatting output from a text::table. I've come to find text tables to be very user friendly when it comes to displaying and being able to copy and paste information in a specific format. However, I'm only familiar with viewing their output in text files or in terminal. When it comes to printing them to an HTML file I'm lost because they lose all their nice alignment. I like the way it organizes information like a table in HTML without the hassle of having jumbled text when copying to an editor. If someone could help me figure out how to export a text table to html or could direct me to an overall better way of doing things that would be fantastic. Thanks for reading this and I appreciate your response!

Replies are listed 'Best First'.
Re: Text::Table formatting with HTML
by jeffa (Bishop) on Jul 10, 2015 at 20:57 UTC

    You could try parsing the textual output from Text::Table but if you are the one controlling the program, then you already have the data you want to wrap into an HTML table. From the Text::Table docs:

    use Text::Table; my $tb = Text::Table->new( "Planet", "Radius\nkm", "Density\ng/cm^3" ); $tb->load( [ "Mercury", 2360, 3.7 ], [ "Venus", 6110, 5.1 ], [ "Earth", 6378, 5.52 ], [ "Jupiter", 71030, 1.3 ], ); print $tb;

    If you already have the data in memory, then you can instead try something like so (using my shiny new Spreadsheet::HTML)

    use Spreadsheet::HTML qw( generate ); my @data =( ["Planet", "Radius\nkm", "Density\ng/cm^3"], [ "Mercury", 2360, 3.7 ], [ "Venus", 6110, 5.1 ], [ "Earth", 6378, 5.52 ], [ "Jupiter", 71030, 1.3 ], ); print generate( data => \@data );

    Spreadsheet::HTML can attempt to load data from a number of file formats but plain old text is not one of them because there are very many different possibilities. It is certainly possible, but likely at the expense of making my API more complex. Hope this helps! :)

    P.S. I just released v0.36 which fixes a bug introduced in v0.34 in which the animate feature was breaking. This new release also features the ability to produce Handsontable tables, which are extremely slick Excel-like data grids. Very nice stuff.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thank you so much this helped a ton and was exactly what I was looking for!!!

        You are welcome. I have this sort of passion for HTML tables. :)

        If you are processing very large tables then you would be much better off printing out your own tags or even better, using a templating solution like Template:

        use strict; use warnings; use Template; my @data =( ["Planet", "Radius\nkm", "Density\ng/cm^3"], [ "Mercury", 2360, 3.7 ], [ "Venus", 6110, 5.1 ], [ "Earth", 6378, 5.52 ], [ "Jupiter", 71030, 1.3 ], ); my $tmpl = '<table>[% FOREACH row = rows %] <tr>[% FOREACH cell = row %] <td>[% cell %]</td>[% END %] </tr>[% END %] </table> '; my $table = Template->new; my $html = ''; $table->process( \$tmpl, { rows => \@data }, \$html ) or warn $table-> +error, $/; print $html;

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Text::Table formatting with HTML
by 1nickt (Canon) on Jul 10, 2015 at 19:38 UTC

    Wow, there is an almost unlimited selection of templating tools on CPAN. You must do some research because asking here will only get opinions. You need to look at the different options and see which have the features you need.

    You could do what you want right now, which is to replicate a simple text table, with almost all of the tools (as well as with sprintf). You need to plan ahead. Will you need to export as JSON? As HTML? As a PDF? If your requirements can grow, then start with an expandable toolkit. If you think you can do with a specialized tool that just speaks one or a subset of outputs, use that.

    The Template Toolkit is the complete solution, with full plugin architecture and lots of contributing plugin authors.

    Last thing I built with templates I used Template::Tiny and its own plugins, mostly because it installed without compiling IIRC, so the user on a shared host could run code written with it. You could try starting with that.

    Good luck in your search :-)

    update: be more specific in my "opinion-recommendation" :)

    Remember: Ne dederis in spiritu molere illegitimi!
Re: Text::Table formatting with HTML
by marinersk (Priest) on Jul 10, 2015 at 22:52 UTC

    If you're looking for fastest effect, I second jeffa's solution above.

    If you'd like to learn the raw HTML of doing tables by hand, I'd recommend a quick study at W3Schools HTML Tables.

    The modern approach should probably include the use of CSS.

Re: Text::Table formatting with HTML
by Anonymous Monk on Jul 10, 2015 at 23:17 UTC
    yunglean808 signs up and makes his first post just in time to be the perfect advert for Jeffa's latest module.

    How convenient.

      Nobody can agree with everyone's everything, but if it works, it works. If it works well, kudos to the module author, regardless of who it is and what prejudice might exist.

      If someone else has code that makes my life easier and I don't necessarily see eye-to-eye with that person, I'm going to use their code, thank them, give feedback and otherwise be appreciative for the assistance. I can beef with them outside of the scope of their good work.

      -stevieb