English 中文(简体)
Perl搜索只显示最后一个结果
原标题:Perl search is only showing last result
  • 时间:2012-05-27 15:57:37
  •  标签:
  • perl
  • search

我有两个数组,一个包含搜索词,另一个是从文件中提取的多行。我有一个嵌套的foreach语句,正在搜索所有组合,但只有最后一个匹配项显示出来,尽管我知道还有许多其他匹配项!!我尝试了许多不同版本的代码,但这是我的最后一个版本:

open (MYFILE,  searchTerms.txt );
open (MYFILE2,  fileToSearchIn.xml );

@searchTerms = <MYFILE>;
@xml = <MYFILE2>;

close(MYFILE2);
close(MYFILE);
$results = "";

foreach $searchIn (@xml)
{
    foreach $searchFor (@searchTerms)
    {
        #print "searching for $searchFor in: $searchIn
";
        if ($searchIn =~ m/$searchFor/)
        {
            $temp = "found in $searchIn 
 while searching for: $searchFor ";
            $results = $results.$temp."
";
            $temp = "";
        }
    }
}

print $results;
最佳回答

你应该在程序开始时始终使用严格的和使用警告my.声明所有变量。这尤其适用于您请求代码帮助的情况,因为此措施可以快速发现许多简单的错误。

As Raze2dust has said it is important to remember that lines read from a file will have a trailing newline " " character. If you were checking for exact matches between a pair of lines then this wouldn t matter, but since it s not working for you I assume the strings in searchTerms.txt can appear anywhere in the lines of fileToSearchIn.xml. That means you need to use chomp the strings from searchTerms.txt; lines from the other file can stay as they are.

通过使用文件::Slurp模块。它为您完成所有的文件处理,如果您要求,它将从输入文本中抓取任何换行符。

我已经将你的程序更改为使用此模块,这样你就可以看到它是如何工作的。

use strict;
use warnings;

use File::Slurp;

my @searchTerms = read_file( searchTerms.txt , chomp => 1);
my @xml = read_file( fileToSearchIn.xml );

my @results;

foreach my $searchIn (@xml) {
  foreach my $searchFor (@searchTerms) {
    if ($searchIn =~ m/$searchFor/) {
      push @results, qq/Found in "$searchIn"
 while searching for "$searchFor"/;
    }
  }
}

print "$_
" for @results;
问题回答

chomp您的输入以删除换行符:

open (MYFILE,  searchTerms.txt );
open (MYFILE2,  fileToSearchIn.xml );

@searchTerms = <MYFILE>;
@xml = <MYFILE2>;

close(MYFILE2);
close(MYFILE);
$results = "";

foreach $searchIn (@xml)
{
    chomp($searchIn);
    foreach $searchFor (@searchTerms)
    {
        chomp($searchFor);
        #print "searching for $searchFor in: $searchIn
";
        if ($searchIn =~ m/$searchFor/)
        {
            $temp = "found in $searchIn 
 while searching for: $searchFor ";
            $results = $results.$temp."
";
            $temp = "";
        }
    }
}

print $results;

Basically, you are thinking you are searching for a , but actually it is searching for a because that is how it reads the input unless you use chomp. It matches only if a is the last character because in that case, it will be succeeded by a newline.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签