How to find difference between two arrays in Perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find difference between two arrays in Perl?
# 1  
Old 09-02-2009
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 = [1,5,15,20];
@Array2 = [15,1];

I would like to find difference between these two and generate third array that contains the difference. In this case it would look like -

Array3 - [5,20];

Could anyone please help how to do this in perl?

Thanks,

---------- Post updated at 05:33 PM ---------- Previous update was at 04:40 PM ----------

Nevermind. I found the solution. This is what I am doing now -

foreach $wrkelement (@Array1, @Array2) { $count{$element}++ }
foreach $element (keys %count) {
push @{ $count{$element} > 1 ? \@Intersection : \@Difference }, $element;
}


@Difference is the Array3 I was looking for - difference between values in two arrays Smilie

Though, if someone has any better way of doing then plz let me know. I would love to know!

Thanks,
# 2  
Old 09-02-2009
Code:
$
$ perl -le '@x=(1,5,15,20); @y=(15,1); %y=map{$_=>1} @y;
>           @diff=grep(!defined $y{$_}, @x);
>           print foreach (@diff)'
5
20
$

tyler_durden
# 3  
Old 09-02-2009
No loops are needed:
Code:
my @a1, @a2, %diff1, %diff2;
@a1 = (1, 5, 10, 15);
@a2 = (5, 15, 25);

@diff1{ @a1 } = @a1;
delete @diff1{ @a2 };
# %diff1 contains elements from '@a1' that are not in '@a2'

@diff2{ @a2 } = @a2;
delete @diff2{ @a1 };
# %diff2 contains elements from '@a2' that are not in '@a1'

@k = (keys %diff1, keys %diff2);
print "keys = @k\n";

In @k are the values that make up the difference.
# 4  
Old 09-03-2009
A bit off-topic, but it may be interesting to know that Ruby has the most intuitive solution for this problem:

Code:
$
$ ruby -e 'a1 = [1,5,15,20]; a2 = [15,1]; puts a1 - a2'
5
20
$
$ ruby -e 'puts [1,5,15,20] - [15,1]'
5
20
$

tyler_durden
# 5  
Old 09-03-2009
Hey, that is pretty cool. Smilie One more reason to put Ruby on my list of languages to play around with...
# 6  
Old 09-04-2009
Its all there available, don't reinvent the wheel Smilie

Try this,
List::Compare - search.cpan.org

---------- Post updated at 09:40 AM ---------- Previous update was at 09:07 AM ----------

Quote:
Originally Posted by sncoupons
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 = [1,5,15,20];
@Array2 = [15,1];

I would like to find difference between these two and generate third array that contains the difference. In this case it would look like -

Array3 - [5,20];

Could anyone please help how to do this in perl?

Thanks,

---------- Post updated at 05:33 PM ---------- Previous update was at 04:40 PM ----------

Nevermind. I found the solution. This is what I am doing now -

foreach $wrkelement (@Array1, @Array2) { $count{$element}++ }
foreach $element (keys %count) {
push @{ $count{$element} > 1 ? \@Intersection : \@Difference }, $element;
}


@Difference is the Array3 I was looking for - difference between values in two arrays Smilie

Though, if someone has any better way of doing then plz let me know. I would love to know!

Thanks,

Is this working?
1. Context used is wrong
2. First loop should use $wrkelement and $element

Or, was that just a sample?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: find next available lowest number that is available in two arrays

Hi there. I have a number allocation problem whereby I have 2 arrays built from 2 different sources. The arrays will just contain a listed of sorted numbers @a 1 7 10 14 15 16 @b 1 7 10 11 14 15 16 (2 Replies)
Discussion started by: hcclnoodles
2 Replies

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

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

4. Shell Programming and Scripting

Difference between 2 arrays in perl

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... (3 Replies)
Discussion started by: vanitham
3 Replies

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

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

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 functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
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