Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在perl中匹配两个数组相同位置的元素_Arrays_Perl_Compare_Equals_Match - Fatal编程技术网

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

Arrays 在perl中匹配两个数组相同位置的元素,arrays,perl,compare,equals,match,Arrays,Perl,Compare,Equals,Match,我有两个数组 @a = qw/ A B C D E /; @b = qw/ B B C A /; @a=qw/a B C D E/; @b=qw/b b C A/; 我需要检查相同的元素是否出现在每个数组的相同位置 e.g. $a[2] = "B"; $b[3] = "C"; 例如 $a[2]=“B”; $b[3]=“C”; 如果是,请计算发生这种情况的次数 它需要忽略任何空白元素 e.g. $a[6] = ; $b[6] =

我有两个数组

@a = qw/ A B C D E /; @b = qw/ B B C A /; @a=qw/a B C D E/; @b=qw/b b C A/; 我需要检查相同的元素是否出现在每个数组的相同位置

e.g. $a[2] = "B"; $b[3] = "C"; 例如 $a[2]=“B”; $b[3]=“C”; 如果是,请计算发生这种情况的次数 它需要忽略任何空白元素

e.g. $a[6] = ; $b[6] = ; 例如 $a[6]=; $b[6]=; 评论是最感谢我喜欢了解脚本

我试过intersecteq==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
#/usr/bin/perl-w
我的@a=;
我的@b=;
$occurs=0#使用eq
我的每封$信(@a){
如果(我的$letter2(@a)eq$letter){#这里有语法错误
$count++;
}
}#此处出现语法错误

如前所述,对于您的问题,我会使用以下方法:

$happened += $a[$_] eq $b[$_] for 0 .. $#a;
要忽略空元素,还可以检查
已定义($a[$\u])
长度($a[$\u])

但是,

qw/ B B C       A /
qw/B C A/
相同,因此不引入空元素。同样,同一元素的示例在不同位置显示了不同的元素。

此处空白的定义需要澄清。它是一个空字符串吗?
undef
defined值?空白

在下面的示例中,我假设任何没有非空白字符的元素都为空,这说明了如何使用
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

请发布您尝试的代码,并准确解释哪些代码不适用于它。#/usr/bin/perl-w my@a=(“a”、“B”、“C”、“D”、“E”、“u”、“u”);my@b=(“b”,“b”,“C”,“A”)$发生率=0#使用eq foreach my$letter(@a){如果(my$letter2(@a)eq$letter){#语法错误在这里$count++}}}语法错误,如果没有在注释中发布代码,则完全无法读取。请检查我所做的编辑,它可能不完全是您所拥有的。对不起,我是新来的。是的,不是。这是更广泛问题的一部分,我从文件中读取数组的内容。对于每个循环,都将类似于that@user1044326:如果需要比较文件,请说明。比较这样的数组类似于在计算机时代发送电报。如果
keys@array
语法被标记为无效,则您的Perl版本是5.12之前的版本。0@MonteCristo:我的错。用
每个
键替换
,非常感谢。你能解释一下为什么我需要在值和索引上循环,而不是只循环值,即“foreach my$letter(@a)”?