在perl中匹配2个数组相同位置的元素

发布于 2024-12-15 10:01:30 字数 653 浏览 1 评论 0原文

我有 2 个数组,

    @a = qw/ A B C D E     /;
    @b = qw/ B B C       A /;    

我需要检查相同的元素是否出现在每个数组的相同位置,

    e.g. 

    $a[2] = "B";
    $b[3] = "C";

如果是的话,计算发生这种情况的次数 它需要忽略任何空白元素

    e.g.

    $a[6] =  ;
    $b[6] =  ;

注释是最受赞赏的我喜欢理解脚本。

我尝试了 intersect eq == cmp 等,但我不太明白并且不太确定

提前致谢。

到目前为止,这是我的代码:

#!/usr/bin/perl -w

my @a = <FILE1>;
my @b = <FILE2>;

$occurs = 0; #Using eq
foreach my $letter (@a) {
    if (my $letter2 (@a) eq $letter) { #Syntax error here
        $count ++;
    }
} #syntax error here

I have 2 arrays

    @a = qw/ A B C D E     /;
    @b = qw/ B B C       A /;    

I need to check if the same element appear in the same position of each array

    e.g. 

    $a[2] = "B";
    $b[3] = "C";

if so count the number of times this happened
it need to disregard any blank elements

    e.g.

    $a[6] =  ;
    $b[6] =  ;

comments are most appreciated I like to understand the script.

I tried intersect eq == cmp etc but I can't quite get it and not quite sure

Thanks in advance.

Here's my code so far:

#!/usr/bin/perl -w

my @a = <FILE1>;
my @b = <FILE2>;

$occurs = 0; #Using eq
foreach my $letter (@a) {
    if (my $letter2 (@a) eq $letter) { #Syntax error here
        $count ++;
    }
} #syntax error here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-12-22 10:01:31

对于您所述的问题,我会使用如下内容:

$happened += $a[$_] eq $b[$_] for 0 .. $#a;

要忽略空元素,您可以检查 define($a[$_])length($a[$_ ]) 也是如此。

然而,

qw/ B B C       A /

qw/BBCA/相同,因此不引入空元素。此外,您的相同元素示例在不同位置显示了不同元素。

For your problem as stated, I'd use something like this:

$happened += $a[$_] eq $b[$_] for 0 .. $#a;

To disregard empty elements, you can check for defined($a[$_]) and length($a[$_]) as well.

However,

qw/ B B C       A /

is the same as qw/B B C A/, so no empty elements are introduced. Also, your example for the same element shows different elements at different positions.

小帐篷 2024-12-22 10:01:31

这里需要澄清空白的定义。是空字符串吗?未定义的值?空白?

我假设任何没有非空白字符的元素在下面的示例中为 blank,该示例显示了如何使用 each @array 构造来完成此操作:

use strict;
use warnings;

my @a = ( 'A', 'B', 'C', 'D', 'E', ' ', ' ' );  # Can't use qw/ / 'cos
my @b = ( ' ', 'B', 'C', ' ', ' ', ' ', 'A' );  # it ignores whitespace

my %count;                                      # Store results in a hash

while ( my ( $index, $value ) = each @a ) { # Loop over index & value together

    my $otherValue = $b[$index];            # Get the other value in @b

    next unless $value =~ /\S/ and $otherValue =~ /\S/;  # Skip if 'blank'

    $count{$value}++ if $value eq $otherValue; # Increment counter for that value
}

print "$_ : $count{$_}\n" for keys %count;     # B : 1
                                               # C : 1

# Find out total

use List::Util 'sum';                          # No need to reinvent wheel
print "Sum : ", sum ( values %count ), "\n";   # Sum : 2

The definition of blank here needs to be clarified. Is it an empty string? An undefined value? Whitespace?

I'm assuming any element with no non-whitespace characters as blank in the example below, which shows how it could be done with the each @array construct:

use strict;
use warnings;

my @a = ( 'A', 'B', 'C', 'D', 'E', ' ', ' ' );  # Can't use qw/ / 'cos
my @b = ( ' ', 'B', 'C', ' ', ' ', ' ', 'A' );  # it ignores whitespace

my %count;                                      # Store results in a hash

while ( my ( $index, $value ) = each @a ) { # Loop over index & value together

    my $otherValue = $b[$index];            # Get the other value in @b

    next unless $value =~ /\S/ and $otherValue =~ /\S/;  # Skip if 'blank'

    $count{$value}++ if $value eq $otherValue; # Increment counter for that value
}

print "$_ : $count{$_}\n" for keys %count;     # B : 1
                                               # C : 1

# Find out total

use List::Util 'sum';                          # No need to reinvent wheel
print "Sum : ", sum ( values %count ), "\n";   # Sum : 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文