2

我的阵列有问题,需要您的帮助。

我想用 my 给出的结果创建一个数组if,以便与另一个数组进行比较。

if (grep {$community eq $_ } @communities) {
   my $community_matchs = "";
   print "$community;$login;$last_at;;$size;1\n";
   push (@matchs, $community_matchs); 
   #my array
}
else{
   print "$community;$login;$last_at;;$size;0\n";
}

然后,以后

if (grep {$c ne $_} @matchs) {
   print "$community;3\n";

我是初学者和法国人,所以请理解我。

4

2 回答 2

1

您可以使用 Data::Dumper 进行调试。

use Data::Dumper;
print 'matchs:'.Dumper(\@matchs);

您没有添加任何内容,@matchs因此它将为空。

也许这就是你要找的:

if (my @community_matchs = grep {$community eq $_ } @communities) {
   print "$community;$login;$last_at;;$size;1\n";
   push (@matchs, @community_matchs); 
   #my array
}
于 2013-09-30T11:59:07.610 回答
0

只是我认为你正在尝试做的一个例子,但我会考虑这样重写:

#!/usr/bin/perl
use strict;
use warnings;

my @communities = qw(community1 community2 community3 community4 community5 community6 community7 community8 community9);

my $community = 'community6';

my @matches;

foreach (@communities){
    if ($_ eq $community) {
        print "Match: $_\n";
        push (@matches, $_);
        # Do something else...
    }
    else {
        print "No match: $_\n";
        # Do something else...
    }
}

输出:

No match: community1
No match: community2
No match: community3
No match: community4
No match: community5
Match: community6
No match: community7
No match: community8
No match: community9
于 2013-09-30T12:23:51.077 回答