为什么我的Perl程序找不到Array :: Utils?

时间:2013-12-27 06:08:49

标签: arrays perl module cpan

use Array::Utils qw(:all);# it showing error
my @array1 = (1, 2, 3, 5, 7, 23, 8, 14, 95, 19);
my @array2 = (3, 14, 6, 22, 88, 19, 100);
my@isect = intersect(@array1,@array2);
print @isect,$/;

use Array::Utils qw(:all);#它显示错误。Can't locate Array/Utils.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at grep_exr.pl line 5.。为什么它显示此Error.whats我的代码中出错了?或者我们必须要做的任何其他方法。请告诉我们。您的答案将会受到欢迎。

2 个答案:

答案 0 :(得分:4)

就像它说的那样,找不到模块。可能是因为你从未安装过它

cpan Array::Utils

或者您可以编写自己的解决方案

my %array1 = map { $_ => 1 } @array1;
my @intersect = grep { $array1{$_} } @array2;

答案 1 :(得分:0)

如果不使用模块,那该怎么办:

#!/usr/bin/perl -w                                                                                                                      

my @union = @intersection = @difference = ();
my %count = ();

my @array1 = (1, 2, 3, 5, 7, 23, 8, 14, 95, 19);
my @array2 = (3, 14, 6, 22, 88, 19, 100);

foreach my $element (@array1, @array2) {
  $count{$element}++
 }

foreach my $element (keys %count) {
   push @union, $element;
   push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}

foreach my $k ( keys %count ) {
  if ( $count{$k} > 1 ) {
    print "$k exist on both the arrays\n";
  }
}

顺便说一句,如果你想安装Array :: util,请从以下网址下载tar文件:http://search.cpan.org/dist/Array-Utils/Utils.pm

以root身份执行以下步骤。

1. Untar it.
2. Run 
   2.1 perl Makefile.PL
   2.2 make test
   2.3 make install