【发布时间】:2013-06-11 23:19:45
【问题描述】:
我有一个通过另一个脚本调用的子例程来读取 HTML 文件。下面是代码。
sub read_html
{
$data=`cat "$_[0]"`;
use HTML::TableExtract;
print "CALLING read_html to read $_[0]\n";
#my $self = shift;
print "$_[1]";
$te = HTML::TableExtract->new( headers => [($_[1])] );
$te->parse($data);
my $line_cnt=0;
# Examine all matching tables
foreach $ts ($te->tables)
{
if ($ts->rows ne "")
{
foreach $row ($ts->rows)
{
foreach (@$row) { $_='' unless defined $_; }
print @$row;
if (@$row[0] ne ' ' and @$row[0] ne '' and
@$row[0] ne "\n" and @$row[0] ne "\t")
{
$line_cnt++;
}
}
}
return $line_cnt;
}
}
当我运行上面的脚本时,当标题作为变量传递时,它没有显示 HTML 表格数据。
$te = HTML::TableExtract->new( headers => [($_[1])] );
但是,如果我将表达式 $_[1] 替换为如下所示的硬编码值,它将返回指定标题下的所有列值
$te = HTML::TableExtract->new(
headers => [("PO Number",
"Invoice Number",
"DC Number",
"Store Number",
"Invoice Amount",
"Discount",
"Amount Paid")] );
我将子例程称为read_html($file, $headers),其中$file 是文件名,$headers 保存标头值,逗号分隔。
任何帮助将不胜感激。
【问题讨论】:
标签: perl html-table html-tableextract