简体   繁体   English

将字符串数组与 perl 中的另一个字符串数组进行比较

[英]compare an array of string with another array of strings in perl

I want to compare an array of string with another array of strings;我想将一个字符串数组与另一个字符串数组进行比较; if it matches, print matched.如果匹配,打印匹配。

Example:例子:

@array = ("R-ID 1.0001", "RA-ID 61.02154", "TCA-ID 49.021456","RCID 61.02154","RB-ID 61.02154");
@var = ("TCA-ID 49", "R-ID 1");

for (my $x = 0; $x <= 4; $x++)
{
  $array[$x] =~ /(.+?)\./;
  if( ($var[0] eq $1) or ($var[1] eq $1) )
  {
    print "\n deleted rows are :@array\n";
  }
  else
  {
    print "printed rows are : @array \n";
    push(@Matrix, \@array);
  }

Then I need to compare @var with the @array ;然后我需要将@var@array进行比较; if it is matched, print the matched pattern.如果匹配,则打印匹配的模式。

Here the entire logic is in a hireartical for loop which gives a new @array in each iteration.这里的整个逻辑都在一个循环中,在每次迭代中给出一个新的@array。 so every time this logic is executed @array has different strings.所以每次执行这个逻辑时@array 都有不同的字符串。

Then comes with @var it is user input field, this @var can be of any size.然后带有@var 是用户输入字段,这个@var 可以是任意大小。 So in order to run the logic according to these constraints, I need to iterate the condition inside the if loop when the user input @var size is 3 for example.因此,为了根据这些约束运行逻辑,例如,当用户输入 @var 大小为 3 时,我需要在 if 循环内迭代条件。

So the goal is to match and delete the user input stings using the above mentioned logic.因此,目标是使用上述逻辑匹配和删除用户输入字符串。 But unfortunately tis logic is not working.但不幸的是,这个逻辑不起作用。 Could you please help me out in this issue.你能帮我解决这个问题吗?

The builtin grep keyword is a good place to start.内置的grep关键字是一个很好的起点。

my $count = grep { $_ eq $var } @array;

This returns a count of items ( $_ ) in the array which are equal ( eq ) to $var .这将返回数组中等于 ( eq ) 的项 ( $_ ) 的计数$var

If you needed case-insensitive matching, you could use lc (or in Perl 5.16 or above, fc ) to do that:如果您需要不区分大小写的匹配,您可以使用lc (或在 Perl 5.16 或更高版本中, fc )来执行此操作:

my $count = grep { lc($_) eq lc($var) } @array;

Now, a disadvantage to grep is that it is counting the matches.现在, grep的缺点是它正在计算匹配项。 So after if finds the first match, it will keep on going until the end of the array.所以在 if 找到第一个匹配后,它会一直继续下去,直到数组的末尾。 You don't seem to want that, but just want to know if any item in the array matches, in which case keeping on going might be slower than you need if it's a big array with thousands of elements.您似乎不想要那样,而只是想知道数组中的任何项目是否匹配,在这种情况下,如果它是一个包含数千个元素的大数组,继续进行可能会比您需要的慢。

So instead, use any from the List::Util module (which is bundled with Perl).因此,改为any List::Util 模块(与 Perl 捆绑在一起)中的任何一个。

use List::Util qw( any );
my $matched = any { $_ eq $var } @array;

This will match as soon as it finds the first matching element, and skip searching the rest of the array.这将在找到第一个匹配元素后立即匹配,并跳过搜索数组的 rest。

Here is a couple of versions that allows multiple strings to be matched.这里有几个版本可以匹配多个字符串。 Not clear what form $var takes when you want to store multiple, so assuming they are in an array @var for now.不清楚当你想存储多个时$var采用什么形式,所以假设它们现在在数组@var中。

The key point is this one is the use of the lookup hash to to the matching.关键是这个是使用lookup hash 来匹配。

use strict;
use warnings;

my @var   = ("TCA-ID 49", "RA-ID 61");
my @array = ("R-ID 1", "RA-ID 61", "TCA-ID 49");

# create a lookup for the strings to match
my %lookup = map { $_ => 1} @var ;

for my $entry (@array)
{
    print "$entry\n" 
        if $lookup{$entry} ;
}

running gives跑步给

RA-ID 61
TCA-ID 49

Next, using a regular expression to do the matching接下来,使用正则表达式进行匹配

use strict;
use warnings;

my @var   = ("TCA-ID 49", "RA-ID 61");
my @array = ("R-ID 1", "RA-ID 61", "TCA-ID 49");

my $re = join "|", map { quotemeta } @var;

print "$_\n" for grep { /^($re)$/ } @array ;

output is the same output是一样的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM