Results 1 to 3 of 3
Hello
Thanks everyone for the help earlier, what I would like to learn now is how can I achieve the following :
array1 = (1234567,7665456,998889,000909)
array2 = (1234567,5581445,998889,000909)
Result
5581445 ...
- 01-27-2012 #1Just Joined!
- Join Date
- Aug 2009
- Location
- Toronto
- Posts
- 31
check the difference between 2 arrays
Hello
Thanks everyone for the help earlier, what I would like to learn now is how can I achieve the following :
array1 = (1234567,7665456,998889,000909)
array2 = (1234567,5581445,998889,000909)
Result
5581445 doesn't exist on array1
Thank you
- 01-27-2012 #2
There are several ways you could do this.
There's you could iterate over the first array, searching the second array for each entry in the first, then repeat with the arrays swapped.
A second way, which would be more memory intensive but probably much quicker for large arrays would be to sort them both (or a copy of both) then iterate over them both together comparing elements, from each, listing the lower value of the two in each comparison then incrementing the index in that array only. If you find a match then increment both. When one array runs out of elements you list all the elements in the array with elements left.Linux user #126863 - see http://linuxcounter.net/
- 01-28-2012 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Much of the heavy lifting can be done with module List::Compare:
producing:Code:#!/usr/bin/env perl # @(#) p1 Demonstrate module List::Compare. use strict; use warnings; use List::Compare; my (@array1) = qw(1234567 7665456 998889 000909); my (@array2) = qw(1234567 5581445 998889 000909); # Print arrays. print " array 1 = \"@array1\"\n"; print " array 2 = \"@array2\"\n"; print "\n"; # Create comparison object. my ($lc) = List::Compare->new( \@array1, \@array2 ); my ( @lonly, @ronly ); # Calculate differences, left and right. @lonly = $lc->get_unique; print " 1,2 = \"@lonly\"\n"; @ronly = $lc->get_complement; print " 2,1 = \"@ronly\"\n"; exit(0);
The context for me is:Code:% ./p1 array 1 = "1234567 7665456 998889 000909" array 2 = "1234567 5581445 998889 000909" 1,2 = "7665456" 2,1 = "5581445"
In Debian, code often is installed with apt-get. In this case, the package name is liblist-compare-perl, found with apt-cache search compare | grep perl. After that, one can use perldoc List::Compare to see the documentation:Code:OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) perl 5.10.0 0.37 List::Compare
Best wishes ... cheers, drlCode:List::Compare(3) User Contributed Perl Documentation List::Compare(3) NAME List::Compare - Compare elements of two or more lists ...Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote