Hi,

I have 2 arrays and i have to compute the difference between arrays in perl. Here is the code.

@a=qw(20095053 19947571 19933274 19910458 19875498 19863670 19747485 19699809 19609242 19602509 19400988 19260844 19190289 19067769 19064710 19056771 19036087 18698092 18692784 18687424 18570886 18562159 18478156 18462614 18425777 18292846 18292840 18390561 18381856 18343090 18314125 18309897 18299351 18256328 18211299 18062861 18040982 18032407 18003625 17943087 17868268 17852044 17612870 17573847 17494104 17491138 17314400 17210951 17151357 17081539 17011559 17011555 16989827 16952514 16871843 16870950 16632464 16535754 16535752 16535747 16535726 16535696 16535689 16535686 16499875 16474017 16458478 16291969 16199539 16192297 16156795 16055721 16052476 15939171 15849227 15705617 15691336 15670415 15590839 15452126 15345679 15236322 15163613 15100385 14756422 14701833 14680821 14526123 12799737 12780546 12775710 12771233 12721220 12721213 12620939 12569175 12556490 12411606 12297523 12117816 11914920 11732576 11574659 11564883 11369609 11326282 11315969 11278223 11272792 11087830 11087683 11062271 11020524 10877105 10849508 10824979 10772793 10694268 10684815 10659848 10626296 10497349 10390541 10207114 10202889 10194639 10099984 10065790 9919312 9851510 9827065 9804968 9778516 9723181 9721604 9669523 9405659 9356170 16535648 16535637 16535634 9230505 9147242 9107674 16535567 16535563 16535556 16535553 16535544 16535529 16535523 16535514 16535512 9043106 16535498 10464636 9337410 9239704 8986696 16535468 16535375 8660926 16535341 16535334 8649414 8662749 16535325 16535269 8850557 8735951 16535208 16535206 16535205 8618919 16535195 16535191 16535189 16535184 16535182 16535175 7489899 16535166 16535132 16535124 16535122 16535117 16535103 16535083 16535067 16535064 7744952 7721781 16534951 7865133 8777312 7740161 7612927 7983046 7714151 7959732 7907489 7505880 8260540 8373804 8226304 8332082 8101063 1395729 1724877 1916811 1986361 1906796 2081589 2370044 2614060);
@b=qw(20095053 19910458 19875498 19863670 19747485 19609242 19602509 19400988 19260844 19067769 19056771 19036087 18692784 18478156 18462614 18292840 18390561 18343090 18314125 18256328 18211299 18040982 18032407 18003625 17943087 17868268 17852044 17573847 17494104 17491138 17210951 17151357 17081539 17011559 17011555 16989827 16952514 16871843 16870950 16632464 16535689 16499875 16474017 16291969 16199539 16192297 16156795 16055721 15939171 15849227 15705617 15670415 15590839 15452126 15345679 15236322 15163613 14756422 14701833 14680821 14526123 12799737 12775710 12771233 12721220 12721213 12620939 12569175 12411606 12297523 12117816 11914920 11732576 11574659 11564883 11369609 11326282 11315969 11278223 11087830 11087683 11062271 11020524 10877105 10849508 10772793 10694268 10684815 10497349 10390541 10202889 10194639 10099984 10065790 9827065 9804968 9778516 9723181 9669523 9405659 9356170 16535648 16535634 9230505 9147242 9107674 9043106 16535498 10464636 9239704 8986696 8660926 8649414 8662749 8735951 8618919 7489899 16535117 16535103 7744952 7721781 7865133 7612927 7983046 7714151 7959732 7907489 7505880 8373804 8226304 1395729 1724877 1916811 1986361 1906796 2081589 2370044 18381856 8101063 17452629);
  @union = @intersection = @difference = ();
  %count = ();
  foreach $element (@a, @b) { $count{$element}++ }
   foreach $element (keys %count) {
   push @union, $element;
   push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
   }
	$lem=@difference;
	print "<br> difeenec = @difference ----> $lem <br>";
The length of first array is: 215 and the next array is 140  and the difference should be 75 but i am getting 77.

How to get correct difference between two arrays in perl?

RegardS
vanditha

my $x=@a;
my $y=@b;

print $x-$y;

Using a scalar reference to an array COUNTs the number of values. The answer is 75 in this example.

so:

$count=($element, $element2, etc)=@array;

Set count to the number of elements in the array.

It depends what you mean by difference. Your question (but not the Perl script you showed us) implies that by 'difference' you mean the difference between the size of the array having the most elements minus the size of the array having the least elements. If that is what you mean then mitchems script will give you your answer.

But the Perl script you showed us implied that you were looking for some other kind of difference, i.e the size of the list of elements that were in either @a or in @b but not in both. That would not necessarily equal the difference in size of the two arrays, as your question implies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.