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


In reply to Array comparison for 3 arrays by terrylau

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.