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

Hello, I've been using HTML::TableContentParser to extract data from HTML tables. My question is, is there a module that will take the complex data structure referenced and reverse it? I.e. take a parameter that's the reference to the extracted table and turn it back into an HTML string? Thanks Redmage
  • Comment on How do I reverse an extracted HTML table?

Replies are listed 'Best First'.
Re: How do I reverse an extracted HTML table?
by Anonymous Monk on Dec 05, 2011 at 21:39 UTC
      #!/usr/bin/perl -- use strict; use warnings; use CGI qw/ *table *Tr Td /; use HTML::TableContentParser; my $html = <<'HTML'; <table> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>ro</td><td>sham</td><td>bo</td></tr> <tr><td>&lt;ro&gt;</td><td>&lt;sham&gt;</td><td>&lt;bo&gt;</td></tr> </table> HTML #~ my $p = HTML::TableContentParser->new(); #~ my $tables = $p->parse($html); #~ use DDS; die Dump($tables); my $tables = [ { rows => [ { cells => [ { data => 1 }, { data => 2 }, { data => 3 } ] }, { cells => [ { data => 'ro' }, { data => 'sham' }, { data => 'bo' } ] }, { cells => [ { data => '&lt;ro&gt;' }, { data => '&lt;sham&gt;' }, { data => '&lt;bo&gt;' } ] } ] } ]; for my $t (@$tables) { print start_table(); for my $r ( @{ $t->{rows} } ) { print start_Tr(); for my $c ( @{ $r->{cells} } ) { print Td( $c->{data} ); } print end_Tr(); } print end_table(); } __END__ $ perl html.tablecontentparser.to.html.pl |xml_pp <table> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>ro</td> <td>sham</td> <td>bo</td> </tr> <tr> <td>&lt;ro&gt;</td> <td>&lt;sham&gt;</td> <td>&lt;bo&gt;</td> </tr> </table>

      Instead of HTML::TableContentParser , I would standardize on HTML::Tree and xpath, see

Re: How do I reverse an extracted HTML table?
by ww (Archbishop) on Dec 06, 2011 at 02:50 UTC
    ... But I can't help wondering, why?

    Is your extraction procedure destructive? If so, why not make a copy first, and destroy that?

    Don't mean to sound like I'm sniping... but your plan really sounds like the long way around... especially if the original table is less than perfectly regular or uses CSS.

Re: How do I reverse an extracted HTML table?
by locked_user sundialsvc4 (Abbot) on Dec 06, 2011 at 13:27 UTC

    A generalized solution might benefit from using a templating system, such as the ubiquitous Template::Toolkit, to actually do the grunt-work of generating the output HTML once you have “flipped” the data structure.

    Or, perhaps you don’t have to actually flip the thing, because a template can grab data from a function just as easily as it can get it from a variable.   So you could write a tiny function f(x,y) that returned element (y,x) ...

    A template can be trivially changed, as often as (the customer | your boss | the marketing department) wants to change it.   The “magic” comes from the (already in place ...) HTML parser and, maybe, just a handful of clever functions that a template can call.   The issue that is most likely to be (constantly) changed, namely, “tweaking the output,” has been factored-out such that it contributes little or nothing to code complexity and ongoing maintenance.

      WOW, just WOW

      LOL