terrylau has asked for the wisdom of the Perl Monks concerning the following question:
Perl newbie here… need some help and suggestions. I’ve searched for many examples but so far it’s all for array operation for two arrays. I have three arrays and want to find the common value in the three array and list them out :
Array 1 = {0, 1,2,3,4,5,6,7,8,9} Array 2 = {1,2,3,4,6,8, 10, 12,14} Array 3 = {1,2,3,5,7,9, 11,13,15}
I want the output to be something like below:
Array 1 Array 2 Array 3 0 yes 1 yes yes yes 2 yes yes yes 3 yes yes yes 4 yes yes 5 yes yes 6 yes yes 7 yes yes 8 yes yes 9 yes yes 10 yes 11 yes 12 yes 13 yes 14 yes 15 yes
The idea is to list out the output as above. I’ve started with something below which I got from another website but got stuck :
#!/usr/bin/perl use strict; use warnings; my @array1; my @array2; my @diff; my @isect; my $item; my %count; @array 1 = (0, 1,2,3,4,5,6,7,8,9); @array 2 = (1,2,3,4,6,8, 10, 12,14); @isect = ( ); @diff = ( ); %count = ( ); foreach $item (@array1, @array2) { $count{$item}++;} foreach $item (keys %count) { if ($count{$item} == 2) { push @isect, $item; } else { push @diff, $item; } } print "\nA Array = @array1\n"; print "\nB Array = @array2\n"; print "\nIntersect Array = @isect\n"; print "\nDiff Array = @diff\n\n";
Appreciate the help
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array comparison for 3 arrays
by GrandFather (Saint) on Apr 14, 2010 at 04:24 UTC | |
|
Re: Array comparison for 3 arrays
by ikegami (Patriarch) on Apr 14, 2010 at 05:11 UTC | |
|
Re: Array comparison for 3 arrays
by colwellj (Monk) on Apr 14, 2010 at 03:44 UTC | |
|
Re: Array comparison for 3 arrays
by rovf (Priest) on Apr 14, 2010 at 07:32 UTC | |
|
Re: Array comparison for 3 arrays
by Khen1950fx (Canon) on Apr 14, 2010 at 07:22 UTC | |
|
Re: Array comparison for 3 arrays
by PeterPeiGuo (Hermit) on Apr 14, 2010 at 05:49 UTC | |
by GrandFather (Saint) on Apr 14, 2010 at 06:54 UTC | |
by 7stud (Deacon) on Apr 14, 2010 at 07:23 UTC | |
by GrandFather (Saint) on Apr 14, 2010 at 09:16 UTC | |
|
Re: Array comparison for 3 arrays
by rovf (Priest) on Apr 14, 2010 at 07:38 UTC | |
by ikegami (Patriarch) on Apr 15, 2010 at 04:32 UTC | |
by Anonymous Monk on Apr 15, 2010 at 03:54 UTC | |
by rovf (Priest) on Apr 15, 2010 at 10:41 UTC |