生成变量的Perl脚本

时间:2014-05-07 12:19:39

标签: arrays perl variables grep

我正在运行一个从网页中提取链接的脚本,但每当网页上的某些内容发生变化时,网页就会输出不同数量的正确网址,这种情况经常发生在此脚本中。我的约束'是$ tco它确保url onyl以http://t.co

开头
$tconike = (grep /$tco/, @links);
print "$tconike\n";

这决定了满足我需要的网址数量,这打印出了' 2'

my @found = (grep /$tco/, @links);
if (@found) {
    for my $url (@found) {
    print "$found[0]\n";
    print "$found[1]\n";
    }
}

这打印了这个案例的网址有两个。前 http://t.co/5 http://t.co/r 我可以让perl脚本重新标记以t.co开头的url数量,并根据$ tconike(可用URL的数量)输出添加更多变量($ found [0]和$ found [1])?

我需要在使用WWW时访问URL:机械化以填写URL上的表格。

1 个答案:

答案 0 :(得分:2)

正如choroba所说,你不需要这样做。您可以单独使用print "$url\n"而不是每个数组元素,因为它将为@found内的每个元素调用循环块。


如果您仍需要基于网址数量的更多变量,则可以执行以下操作。但这不是您应该在生产中使用的代码。事实上,不能使用它。它有效,但它的目的是作为一个笑话。 :)

my $code;
my @found = (grep /$tco/, @links);
if (@found) {
  for (my $i=0; $i<= $#found; $i++) {
    $code .= q{print "$found[$i]\n"} . qq{\n};
  }
  eval $code for @found; # this is very evil
}