获取哈希内容时出错
Error getting the contents of a hash
我的 perl 代码如下所示:
my $ns = scraper {
process "table.table tr>td>a", 'files[]' => 'TEXT';
};
my $mres = $ns->scrape(URI->new($purlToScrape));
if ( ($#{$mres->{files}}) > 0 ) {
print Dumper($mres->{files} );
foreach my $key (keys %{ $mres->{files} }) {
print @{$mres->{files}}[$key]."\n";
}
}
同时 运行:
$VAR1 = [
'Metropolis (1927)',
'The Adventures of Robin Hood (1938)',
'King Kong (1933)',
'The Treasure of the Sierra Madre (1948)',
'Up (2009)',
'Lawrence of Arabia (1962)',
];
Not a HASH reference at /root/bash-advanced-scripts/rotten.pl line 28.
第 28 行是这样的:
print @{$mres->{files}}[$key]."\n";
我该如何解决这个问题?
错误来自上一行:
foreach my $key (keys %{ $mres->{files} })
$mres->{files}
是一个数组引用,因此您不能将它作为散列取消引用。
只需使用
for my $file (@{ $mres->{files} }) {
print $file, "\n";
}
我的 perl 代码如下所示:
my $ns = scraper {
process "table.table tr>td>a", 'files[]' => 'TEXT';
};
my $mres = $ns->scrape(URI->new($purlToScrape));
if ( ($#{$mres->{files}}) > 0 ) {
print Dumper($mres->{files} );
foreach my $key (keys %{ $mres->{files} }) {
print @{$mres->{files}}[$key]."\n";
}
}
同时 运行:
$VAR1 = [
'Metropolis (1927)',
'The Adventures of Robin Hood (1938)',
'King Kong (1933)',
'The Treasure of the Sierra Madre (1948)',
'Up (2009)',
'Lawrence of Arabia (1962)',
];
Not a HASH reference at /root/bash-advanced-scripts/rotten.pl line 28.
第 28 行是这样的:
print @{$mres->{files}}[$key]."\n";
我该如何解决这个问题?
错误来自上一行:
foreach my $key (keys %{ $mres->{files} })
$mres->{files}
是一个数组引用,因此您不能将它作为散列取消引用。
只需使用
for my $file (@{ $mres->{files} }) {
print $file, "\n";
}