Hi,
I have 2 arrays:
@a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063);
@b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861);
$len=@a;
print "<br> length of array1: $len<br>";
$len1=@b;
print "<br> length of array2: $len1<br>";
The output should be like this:
18309897 17612870 16871843 19947571 18062861
How to get the above output in perl?
Regards
You use the following script to do it.
my @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063);
my @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861);
my $len=@a;
print "<br> length of array1: $len<br>";
my $len1=@b;
print "<br> length of array2: $len1<br>";
my %hash=();
my $k;
my $i=0;
foreach $k (@a)
{
$hash{$k}=$i;
}
foreach $k (@b)
{
unless(exists $hash{$k})
{
print "$k\n";
}
}
You can also do the above operation by this way also.
my @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063);
my @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861);
my $len=@a;
print "<br> length of array1: $len<br>";
my $len1=@b;
print "<br> length of array2: $len1<br>";
foreach $k (@b)
{
unless(grep { $_ eq $k} @a)
{
print "$k\n";
}
}
Using the grep function in perl we can achieve it,
use strict;
use warnings;
use Data::Dumper;
my @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063);
my @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861);
my @result= grep { my $a = $_; not grep { $a eq $_ } @a } @b;
print Dumper(@result);