Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How can I retrieve the link with the highest number in it using Perl?

by virtus (Initiate)
on Jul 10, 2013 at 01:25 UTC ( #1043403=perlquestion: print w/ replies, xml ) Need Help??
virtus has asked for the wisdom of the Perl Monks concerning the following question:

I'm improving an old script that I had that downloaded some wallpapers for me. I need to know how many

pages of wallpapers one category have. Each link has the number of the page as its text, ie:


<a href="/planes-desktop-wallpapers/page/8">8</a> <a href="/planes-desktop-wallpapers/page/9">9</a> <a href="/planes-desktop-wallpapers/page/10">10</a>

So I need to capture the number ten, because then I can know for sure how many pages that category has.

I've been trying something like:

#!/usr/bin/perl -w use strict; use warnings; use WWW::Mechanize; use HTML::TreeBuilder; use List::Util qw(max); ... my $site = "http://www.hdwallpapers.in /planes-desktop- wallpapers.html"; $mech->get($site); my @links = map {$_->text} grep {$_->text =~ m/\d+/} $mech->find_all_links(); print max @links;

Problem is I'm also getting some junk in the array (not only numbers), so I get a compile error.

Can someone please shed some enlightenment here? tnx

Comment on How can I retrieve the link with the highest number in it using Perl?
Select or Download Code
Re: How can I retrieve the link with the highest number in it using Perl?
by toolic (Chancellor) on Jul 10, 2013 at 01:31 UTC
    Maybe use anchors?
    my @links = map {$_->text} grep {$_->text =~ m/^\d+$/} $mech->find_all_links();

      Hi, tnx for your fast reply, now its working. But please, explain me something... why do I have to call the find_all_links function with no arguments to it so my @links array gets filled?

      That was not much clear to me... one friend of mine was helping me with this script

      tnx for your time.

        Why do I have to call the find_all_links function with no arguments to it so my @links array gets filled?
        Perhaps you were distracted by the multi-line statement?

        This:

        my @links = map {$_->text} grep {$_->text =~ m/^\d+$/} $mech->find_all_links();

        is equivalent to this:

        my @links = map {$_->text} grep {$_->text =~ m/^\d+$/} $mech->find_all +_links();

        (When I first read the original, I didn't see the lack of semicolon on line 1, which made me think it was an example from Damian's time reversed variables.)

        Then read right to left.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043403]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-09-18 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls