如何在Perl中比较数组的不同元素?

时间:2021-07-14 22:57:56

I am new to this field. So kindly go easy on me. I have two arrays:

我是这个领域的新手。所以请好好放轻松。我有两个数组:

@array1 = ("ABC321", "CDB672", "PLE89",....);

@array2 = ("PLE89", "ABC678", "LMD789",...);

I want to compare elements of these two different arrays. But, I want to only match letters with letters. So for instance, if arrays are compared, $array[2] element (PLE) should match with $array2[0] (PLE) and similarly $array1[0] (ABC) should match with $array[1] (ABC). I am able to do it one at time but not able to compare all elements of both array at the same time (that is looping the arrays).

我想比较这两个不同数组的元素。但是,我想只用字母匹配字母。因此,例如,如果比较数组,$ array [2]元素(PLE)应与$ array2 [0](PLE)匹配,类似$ array1 [0](ABC)应与$ array [1](A​​BC)匹配。我能够一次做到一次,但无法同时比较两个数组的所有元素(即循环数组)。

    my ($value1)= ($array[2]=~ /([A-Z]+)[0-9]+/);
    print "Value1: $value1 \n";
    my ($value2)= ($array[0]=~ /([A-Z]+)[0-9]+/);
    print "Value2 : $value2 \n";
    if ($value1 eq $value2){
            print " length \n";
    }

Any suggestions on how to do I set up loop for both arrays at the same time?

有关如何为两个阵列同时设置循环的任何建议?

2 个解决方案

#1


You can use a hash as a lookup device and get an O(m+n) solution (where m is the length of array1 and n is the length of array2).

您可以使用散列作为查找设备并获得O(m + n)解决方案(其中m是array1的长度,n是array2的长度)。

#!/usr/bin/perl

use strict;
use warnings;

my @array1 = qw(ABC321 CDB672 PLE89);
my @array2 = qw(PLE89  ABC678 LMD789);

my %seen;

for my $item (@array1) {
    die "not a valid item: $item"
        unless my ($key) = $item =~ /([A-Z]+)/;

    #we are using an array to hold the items in case
    #the same key shows up more than once in an array
    #this code can be simpler if you can guarantee 
    #that the keys are unique
    push @{$seen{$key}}, $item;
}

for my $item (@array2) {
    die "not a valid item: $item"
        unless my ($key) = $item =~ /([A-Z]+)/;
    if (exists $seen{$key}) {
        print "$item is in array1, it matches @{$seen{$key}}\n";
    } else {
        print "$item is not in array1\n";
    }
}

#2


Language-agnostic suggestion would be to sort both arrays first (should take you O(n lg(n)), then compare with two iterators in linear time. If performance is not an issue, just keep it simple and go with quadratic number of pair-wise comparisons. While sorting you can also get rid of digits in the end.

与语言无关的建议是首先对两个数组进行排序(应该使用O(n lg(n)),然后在线性时间内与两个迭代器进行比较。如果性能不是问题,只需保持简单并使用二次数成对比较。排序时你也可以最终摆脱数字。

#1


You can use a hash as a lookup device and get an O(m+n) solution (where m is the length of array1 and n is the length of array2).

您可以使用散列作为查找设备并获得O(m + n)解决方案(其中m是array1的长度,n是array2的长度)。

#!/usr/bin/perl

use strict;
use warnings;

my @array1 = qw(ABC321 CDB672 PLE89);
my @array2 = qw(PLE89  ABC678 LMD789);

my %seen;

for my $item (@array1) {
    die "not a valid item: $item"
        unless my ($key) = $item =~ /([A-Z]+)/;

    #we are using an array to hold the items in case
    #the same key shows up more than once in an array
    #this code can be simpler if you can guarantee 
    #that the keys are unique
    push @{$seen{$key}}, $item;
}

for my $item (@array2) {
    die "not a valid item: $item"
        unless my ($key) = $item =~ /([A-Z]+)/;
    if (exists $seen{$key}) {
        print "$item is in array1, it matches @{$seen{$key}}\n";
    } else {
        print "$item is not in array1\n";
    }
}

#2


Language-agnostic suggestion would be to sort both arrays first (should take you O(n lg(n)), then compare with two iterators in linear time. If performance is not an issue, just keep it simple and go with quadratic number of pair-wise comparisons. While sorting you can also get rid of digits in the end.

与语言无关的建议是首先对两个数组进行排序(应该使用O(n lg(n)),然后在线性时间内与两个迭代器进行比较。如果性能不是问题,只需保持简单并使用二次数成对比较。排序时你也可以最终摆脱数字。