Perl - 捕获出现多于1个数组元素的句子

时间:2017-04-17 18:56:31

标签: arrays perl

我有一个文本文件和一个包含单词列表的数组。我需要找到一种方法,我可以过滤出超过1的句子。我只是无法制定如何编写代码。这是一个例子:

输入:

my @strings = (
    "i'm going to find the occurrence of two words if possible",
    "i'm going to find the occurrence of two words if possible",
    "to find a solution to this problem",
    "i will try my best for a way to this problem"
);

my @words = ("find", "two", "way");

输出:

i'm going to find the occurrence of two words if possible
i'm going to find the occurrence of two words if possible

而且我确实理解这是一个简单的问题,但我的思想似乎遇到了障碍。

1 个答案:

答案 0 :(得分:1)

如果您希望字符串包含两个或更多关键字实例:

my @keywords = ("find", "two", "way");
my %keywords = map { $_ => 1 } @keywords;

for my $string (@strings) {
   my @words = $string =~ /\w+/g;
   my $count = grep { $keywords{$_} } @words;   # Count words that are keywords.
   if ($count >= 2) {
      ...
   }
}

短路交替(即对于极长的琴弦有利):

my @keywords = ("find", "two", "way");
my %keywords = map { $_ => 1 } @keywords;

for my $string (@strings) {
   my $count = 0;
   while ($string =~ /\w+/g) {
      if ($keywords{$_} && ++$count == 2) {
         ...
         last;
      }
   }
}

如果您想要包含两个或更多关键字实例的字符串:

my @keywords = ("find", "two", "way");

for my $string (@strings) {
   my @words = $string =~ /\w+/g;
   my %seen; ++$seen{$_} for @words;
   my $count = grep { $seen{$_} } @keywords;   # Count keywords that were seen.
   if ($count >= 2) {
      ...
   }
}

替代:

my @keywords = ("find", "two", "way");

for my $string (@strings) {
   my @words = $string =~ /\w+/g;
   my %seen = map { $_ => -1 } @keywords;
   my $count = grep { ++$seen{$_} == 0 } @words;
   if ($count >= 2) {
      ...
   }
}

短路交替(即对于极长的琴弦有利):

my @keywords = ("find", "two", "way");

for my $string (@strings) {
   my $count = 0;
   my %seen = map { $_ => -1 } @keywords;
   while ($string =~ /\w+/g) {
      if (++$seen{$_} == 0 && ++$count == 2) {
         ...
         last;
      }
   }
}
相关问题