I parse a string to HTML and extract tables from it.
The tables have two columns: 1st single (key), 2nd multi-value (values)
I want to store the values in a hash to an arrays.
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my $html='
<p class="auto-cursor-target"><br /></p>
<table class="wrapped">
<colgroup><col style="width: 50.0px;" /><col style="width: 29.0px;" />
</colgroup>
<tbody>
<tr><th><p>Wikispace</p></th><th><p>right</p></th></tr>
<tr><td>mimi</td><td>right1</td></tr>
<tr><td colspan="1">mama</td><td colspan="1">right3,right2</td></tr>
</tbody>
</table>
<p class="auto-cursor-target"><br /></p>
';
use HTML::TableExtract;
my $te = HTML::TableExtract->new( headers => [qw(Wikispace right)] );
$te->parse($html);
my %known;
foreach my $ts ($te->tables) {
foreach my $row ($ts->rows) {
print @$row[0], ":::", @$row[1], ": ";
foreach my $val (split(/,/,@$row[1])) {
print $val, ";";
if (! $known{@$row[0]}) {
my @arr = ($val);
@known{@$row[0]}=\@arr;
} else {
# my @arr = \@known{@$row[0]};
# push (@arr, $val);
# print Dumper @arr;
push (@$known{@$row[0]}, $val);
};
}
print "\n";
}
}
print Dumper \%known;
What am I doing wrong? What's wrong with the last push
, and how would you do it differently?
Also is there no way to assign an array directly to a hash (dictionary) instead of first having to generate an array and later linking its address?