in reply to Putting HTML::Element content_list into a hash
Output:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::TokeParser::Simple; my $html = <<'EOHTML'; <table cellspacing="0" cellpadding="2" border="0" width="100%"> <tr> <td width="20%" align="right" valign="top" bgcolor="#CCCCCC"> <font face="arial, helvetica" size="2"><b>02:44 AM EDT</b></font><br> <font face="arial, helvetica" size="1">0:42 (est.)</font><br></td> <td valign="top"><font face="arial, helvetica" size= "1">Speech</font><br> <font face="arial, helvetica" size="2"><a href= "/cspan/cspan.csp?command=dprogram&record=142524675">U.S.-Ja +pan Relations</a><br> Asia Society, Washington Center<br></font> <font face= "arial, helvetica" size="2" color="#CC0000">Ryozo Kato</font> <font face="arial, helvetica" size="1">, Japan</font></td> </tr> </table> EOHTML my $tp = HTML::TokeParser::Simple->new(\$html) or die "Couldn't parse string: $!"; my $start; my @scraped; while (my $t = $tp->get_token) { $start++, next if $t->is_start_tag('table'); next unless $start; my $text = $tp->get_trimmed_text('br'); push @scraped, $text; } my @keys = qw(time length type title org1 org2); my %h_cpan = map {$keys[$_] => $scraped[$_]} (0..$#keys); #for (0..$#keys){ # $h_cpan{$keys[$_]} = $scraped[$_]; #} print Dumper \%h_cpan;
Couple of points.---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl $VAR1 = { 'org2' => 'Ryozo Kato , Japan', 'length' => '0:42 (est.)', 'time' => '02:44 AM EDT', 'org1' => 'Asia Society, Washington Center', 'title' => 'U.S.-Japan Relations', 'type' => 'Speech' }; > Terminated with exit code 0.
Hope that helps.
Update: Changed the for loop to map.
|
|---|