Domanda

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,$/;

The use Array::Utils qw(:all);# it showing error.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..Why it showing this Error.whats the wrong in my code? or any other method we have to do my requirement.Please let us know.your answers will be appreciate.

È stato utile?

Soluzione

Like it says, it can't find the module. Chances are it's because you never installed it

cpan Array::Utils

Or you can just write your own solution

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

Altri suggerimenti

What about this without using module:

#!/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";
  }
}

Btw, if you want to install Array::util, download the tar file from : http://search.cpan.org/dist/Array-Utils/Utils.pm

Do the following steps as root.

1. Untar it.
2. Run 
   2.1 perl Makefile.PL
   2.2 make test
   2.3 make install
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top