http://www.perlmonks.org?node_id=814093

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

hello all

I'm trying to extract table rows from following HTML. It is showing the desired output but is there any other way that I can map my table rows with key-value pair or array of arrays of td elements under a tr

<html><head><title>Person Profile</title></head> <center> <font size=5><b>Profile</b></font> <table cellspacing="1" cellpadding="1"> <tr> <td class="rlab">Short Name:</td> <td class="l">John</td> </tr> <tr> <td class="rlab">Long Name:</td> <td class="l">John Abraham</td> </tr> <tr> <td class="rlab">Company:</td> <td class="l">Idea</a></td> </tr> </tr> <tr> <td class="rlab">Currency:</td> <td class="l">EUR</td> </tr> </table> </body></html>

I'm trying the following code

use strict; use warnings; use HTML::TreeBuilder; #Parse html content using html-treebuilder: my $root = HTML::TreeBuilder->new(); $root->parse($html); $root->eof(); my @tables = $root->look_down(_tag => 'table'); while (@tables) { my $node = shift @tables; if (ref $node) { unshift @tables, $node->content_list; } else { print $node,"\n"; } } $root = $root->delete;

OUTPUT is

---------- Perl ---------- Short Name: John Long Name: John Abraham Company: Idea Currency: EUR Output completed (0 sec consumed) - Normal Termination