Difference between 2 arrays in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between 2 arrays in perl
# 1  
Old 03-04-2010
Difference between 2 arrays in perl

Hi,

I have 2 arrays:

Code:
@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:
Code:
18309897 17612870 16871843 19947571 18062861

How to get the above output in perl?

Regards

Last edited by vanitham; 03-04-2010 at 06:40 AM..
# 2  
Old 03-04-2010
You use the following script to do it.

Code:
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.

Code:
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";
	}
}


Last edited by vivekraj; 03-04-2010 at 06:56 AM..
# 3  
Old 03-04-2010
MySQL

Using the grep function in perl we can achieve it,

Code:
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);

# 4  
Old 03-04-2010
Code:
perl -wle '
@a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063);
@b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861);

map {$r=$_ ; grep { /$r/ } @a or printf "$r " ; } @b'

SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with displaying difference between two arrays (Bash)

The code I have is kind of long, so I'm just posting the part I'm struggling with. I've found many examples online of comparing two arrays in Bash and printing the difference between them. I've tried them all, even mixed and matched some examples with no luck. I know this can't be as hard as I'm... (9 Replies)
Discussion started by: Azrael
9 Replies

2. Shell Programming and Scripting

Arrays in perl

Hi all, I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line. print array......array to a new file. and I have to do it in perl as res of the program in perl. Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies

3. Shell Programming and Scripting

PHP Compare 2 Arrays find difference & case insensitive

Hi, I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1. I don't want to see any instances of array1 wich is not in array2 (here "the") Array1: This, is, the, data, of, array1 Array2: this, is, data, Of,... (2 Replies)
Discussion started by: lowmaster
2 Replies

4. Shell Programming and Scripting

How to find difference between two arrays in Perl?

Hi, Could someone please help me with this? I have two arrays and I need to generate third array containing difference between the two. For example - @Array1 = ; @Array2 = ; I would like to find difference between these two and generate third array that contains the difference. In... (5 Replies)
Discussion started by: sncoupons
5 Replies

5. Shell Programming and Scripting

Compute difference between 2 arrays perl

Hi, I have 2 arrays. I have to compute symmetric difference! @arr=qw(19205134 18630215 18453487 18416242 18338715 18227590 17698645); @arr1=qw(18227590 18053561 17698645 16966777); #The code which i used is this! @union = @isect = @diff = (); %union = %isect = (); %count =... (3 Replies)
Discussion started by: vanitham
3 Replies

6. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

7. Shell Programming and Scripting

perl arrays

Hi I need some help using arrays in perl. I have an array say var and a variable var1. I want to check if the var1 is present in the array. How do I check that ? my @var = 1...10; my $var1 =5; if ( $var1 in @var ) { ....... } else { ....... } Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies

8. Shell Programming and Scripting

Perl - Compare 2 Arrays

Hi all, I have the following script where the contents of file1 and file2 would be something like this: file1: 56790,0,0,100998765 89756,0,0,100567876 867645,1,3,678777654 file2: 56790,0,0,100998765 65776,0,0,4766457890 +5896,0,0,675489876 What I then want to do is check if... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. Shell Programming and Scripting

Perl Arrays and Substituion

@xray =~ s/^ *//g; @xray =~ s/ *$//g; @xray =~ s/\s+/ /g; Guess I have a two part question ... First Is there a way to make substitutions, remove leading spaces, trailing spaces, and crunch multiple spaces into a single space, to the entire array, or must the substitutions be done on on... (1 Reply)
Discussion started by: popeye
1 Replies

10. Programming

perl arrays

hello ppl, i'm coding a perl script and i have the following situation: @array1 = ("test1", "test2", "test3"); @array2 = ("something1", "something2", "something1"); $var1 = "with_one_of_the_array1_values"; $var2 = "with_one_of_the_array2_values"; what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies
Login or Register to Ask a Question