Joel G Mathew
Joel G Mathew

Reputation: 8061

Error getting the contents of a hash

My perl code looks like this:

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"; 
    }
}

While running:

$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.

Line 28 is this:

print @{$mres->{files}}[$key]."\n";

How do I fix this?

Upvotes: 1

Views: 42

Answers (1)

choroba
choroba

Reputation: 241868

The error's coming from the previous line:

foreach my $key (keys %{ $mres->{files} })

$mres->{files} is an array reference, so you can't dereference it as a hash.

Just use

for my $file (@{ $mres->{files} }) {
    print $file, "\n";
}

Upvotes: 5

Related Questions