Joel G Mathew
Joel G Mathew

Reputation: 8061

How to properly use hashes?

I am trying to use the perl Web::Scraper module to scrape a page and process various elements.

I wrote the following script:

use strict;
use warnings;
use Web::Scraper;
use Data::Dumper;
use URI;

my $purlToScrape='https://isohunt.to/torrents/?ihq=back+to+the+future&Torrent_sort=seeders.desc';
my $movcol = scraper {
    process "td.title-row", "movcol[]" => scraper {
        process "span", "title[]" => 'TEXT';
    process "a", "url[]" => '@href';
    };
};

my $details = $movcol->scrape(URI->new($purlToScrape));
print Dumper($details->{movcol});

Output:

$VAR1 = [
  {
    'url' => [
               bless( do{\(my $o = 'https://isohunt.to/torrent_details/5538709/Back-to-the-Future-III-1990-720p-BrRip-x264-700MB-YIFY')}, 'URI::https' ),
               bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
             ],
    'title' => [
                 'Back to the Future III (1990) 720p BrRip x264 - 700MB - YIFY'
               ]
  },
  {
    'url' => [
               bless( do{\(my $o = 'https://isohunt.to/torrent_details/6395538/Back-to-the-Future-1985-1080p')}, 'URI::https' ),
               bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
             ],
    'title' => [
                 'Back to the Future (1985) [1080p]'
               ]
  }
];

What I'm trying to do is to process each title element. How do I use these elements in code?

I tried using print Dumper($details->{movcol}->{title});, but that gave me the error Not a HASH reference

Upvotes: 0

Views: 79

Answers (2)

Borodin
Borodin

Reputation: 126722

The square brackets in the dump indicate arrays, while the braces denote hashes. So you can see that $details->{movcol} is an array of hashes, and each of those hashes has an element with a key title and a value that is another array.

You can print the titles like this

my $movcol = $details->{movcol};

for my $item ( @$movcol ) {
    print $item->{title}[0], "\n";
}

or you can create an array of title strings using

my @titles = map $_->{title}[0], @{ $details->{movcol} };

Upvotes: 1

user1804599
user1804599

Reputation:

$details->{movcol} is an array reference. Dereference the array to get the titles:

for (@{$details->{movcol}}) {
    print "$_->{title}[0]\n";
}

Or, to print just the first title:

print "$details->{movcol}[0]{title}[0]\n";

Upvotes: 2

Related Questions