0
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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
user3138547
  • 11
  • 1
  • 6

2 Answers2

4

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;
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • ikegami,How to install this module? – user3138547 Dec 27 '13 at 06:17
  • I downloaded Array::Utils module from http://search.cpan.org/~zmij/Array-Utils/Utils.pm.please tel me how to install – user3138547 Dec 27 '13 at 06:22
  • `[Ram@localhost work]$ cpan Array::Utils bash: cpan: command not found` – user3138547 Dec 27 '13 at 06:23
  • You have a broken Perl installation – ikegami Dec 27 '13 at 06:23
  • @ ikegami then how to install properly ? – user3138547 Dec 27 '13 at 06:24
  • I would use [`perlbrew`](http://search.cpan.org/perldoc?perlbrew) to easily install a local copy rather than mess with your system installation. – ikegami Dec 27 '13 at 06:26
  • how to install perlbrew? – user3138547 Dec 27 '13 at 06:29
  • I provided a link. Read. – ikegami Dec 27 '13 at 06:29
  • oops, this link better: [App::perlbrew](http://search.cpan.org/perldoc?App::perlbrew) – ikegami Dec 27 '13 at 06:38
  • just a thought, if you don't want to install too many library from cpan, go for python., python supports set and can solve your problem in one line. set1.intersection(set2) – James Sapam Dec 27 '13 at 06:41
  • yopy,I'm using Perl script not python – user3138547 Dec 27 '13 at 06:43
  • @yopy, cause `set1.intersection(set2)` is so much simpler than `grep $array1{$_}, @array2`!?! – ikegami Dec 27 '13 at 06:44
  • `[root@localhost Array-Utils-0.5]# perlbrew init bash: perlbrew: command not found` – user3138547 Dec 27 '13 at 06:44
  • Did you install it? Did you make the change to .bashrc to told you to do? Did you restart your shell to pick up those changes? – ikegami Dec 27 '13 at 06:44
  • Probably don't want to do that as root! I mean, you can, but it kinda defies the purpose – ikegami Dec 27 '13 at 06:46
  • The other option is to install the missing parts of Perl from your distro's package repository. The reason `cpan` is missing is probably cause your distro broke Perl into bits. Your distro might even have a package for Array::Utils – ikegami Dec 27 '13 at 06:50
  • `[Ram@localhost ~]$ curl -kL http://install.perlbrew.pl | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 102 1020 102 1020 0 0 469 0 0:00:02 0:00:02 --:--:-- 2922` – user3138547 Dec 27 '13 at 06:50
  • `## Download the latest perlbrew ## Installing perlbrew perlbrew is installed: ~/perl5/perlbrew/bin/perlbrew perlbrew root (~/perl5/perlbrew) is initialized. Append the following piece of code to the end of your ~/.bash_profile and start a new shell, perlbrew should be up and fully functional from there: source ~/perl5/perlbrew/etc/bashrc Simply run `perlbrew` for usage details. Happy brewing! ## Installing patchperl ## Done. [Ram@localhost ~]$` – user3138547 Dec 27 '13 at 06:51
  • `[Ram@localhost ~]$ perlbrew init bash: perlbrew: command not found [Ram@localhost ~]$ ` here i'm getting error @ ikegami – user3138547 Dec 27 '13 at 06:51
  • Did you read what you posted? **Append the following piece of code to the end of your ~/.bash_profile and start a new shell** – ikegami Dec 27 '13 at 06:54
  • `[root@localhost work]# ~/.bash_profile bash: /root/.bash_profile: Permission denied` – user3138547 Dec 27 '13 at 07:05
0

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
James Sapam
  • 16,036
  • 12
  • 50
  • 73