searching for multiple instances of many strings in Perl

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
DrPL
Forum Commoner
Posts: 26
Joined: Wed Oct 07, 2009 4:22 pm

searching for multiple instances of many strings in Perl

Post by DrPL »

I am writing a tiny perl script using LWP to download webpages which have multiple links in it,
each one having a certain value, such as
<a href="http://www.poobar.com">pleb sauce</a>
, so that I can extract, in this case "pleb sauce". However, there are
many instances of this name in the page and many different names. What I am hoping to
do is to count all instances of "Pleb sauce", "horsehair underpants" etc.

I found this script on a webpage which looks like it might do what I want:

Code: Select all

#!/usr/bin/perl
use strict;
 
my $string = "CATINTHEHATWITHABAT"; // just a test
my $regex = '\wAT';    // This needs to be replaced with an array containing
// all the regexs; in this case, could be '\wHA', '\wTH', etc.
my @matches = ();   
 
while ($string =~ /($regex)/gi){
// do stuff here, increment counter etc.
}
 
But the problem is the while loop; is there a better way to run a script without having to
run through this loop for each regex?

Thanks!

Paul
Post Reply