Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have written some code but it doesn't work. Not sure why:
@array1 = ("fg", "dg", "tu","ph", "dgs",); @array2 = ("dgs","sfd","dg","ph","rhd"); if (scalar(@array1) >= scalar(@array2)) { @bigger_array = @array1; @smaller_array = @array2; } else { @bigger_array = @array2; @smaller_array = @array1; } $value = 0; foreach $word (@bigger_array) { for (my $i = 0; $i <= scalar(@smaller_array); $i++) { if($word eq $array1[$i]) { splice(@smaller_array,$i, 1); $i--; $value++; } } } print $value;
Does anyone know why? Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing arrays and returning number of similar elements
by brian_d_foy (Abbot) on Feb 09, 2006 at 23:25 UTC | |
by Anonymous Monk on Feb 09, 2006 at 23:30 UTC | |
|
Re: Comparing arrays and returning number of similar elements
by Mandrake (Chaplain) on Feb 10, 2006 at 04:43 UTC | |
by Roy Johnson (Monsignor) on Feb 10, 2006 at 15:43 UTC | |
|
Re: Comparing arrays and returning number of similar elements
by planetscape (Chancellor) on Feb 10, 2006 at 07:47 UTC |