开发者

Why does this `while` statement loop endlessly?

I want to print some dates from a site with a structure like this:

<tr><td><b><a href="/calendar.*?=\w+">(.*?)</a></b></td>
    <td align=".*?"/date/(\d+)-(\d+)/">.*?</a> <a href="/year/\d+/">(\d+)</a>开发者_运维技巧;</td>
    <td>(.*?)*</td></tr>

etc.

my $country = $1;
my $month   = $2;
my $day     = $3;
my $year    = $4;
my $event   = $5;

I need to extract only those where the $country is 'USA' but if I use the while statement the code loops endlessly through the first match. How do I rework the script to extract each found USA date?

sub getSpec {
    my $line = shift;
    my $site = getSite($line);
    while ($site =~ s/.../) {
        my $country = $1;
        my $month   = $2;
        my $day     = $3;
        my $year    = $4;
        my $event   = $5;
        if ($country =~ /USA/i) {
            print $month.$date.$year.$country.$event."\n";
        }
    }
}


A global match should make it for you:

   while ($site =~ m/.../g) {

For details, look in the documentation.


It looks like you're not changing the string after the first match. Try reading $site (it's html of the whole site, right?) line by line, so the loop would look like this (my Perl is a little bit rusty, this is only rough sketch, sorry for that):

while ( $_ = another_line_from_$site)
{
   if($_ =~ s/.../) {
    {variables}
     if($country =~ /USA/i) 
       { other_stuff }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜