Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Count of words that match in an array

( #165582=categorized question: print w/ replies, xml ) Need Help??
Contributed by Al Shiferaw on May 10, 2002 at 02:54 UTC
Q&A  > arrays


Description:

Given two arrays, I like to know how many of the values in array A match with the values in Array B.

E.G.

@Array1 = ("A", "B", "C", "D"); @Array2 = ("J", "C", "T", "A");
I want the result to be 2 since both "A" and "C" exist in both arrays.

Answer: Count of words that match in an array
contributed by stephen

use strict; my @Array1 = ("A", "B", "C", "D"); my @Array2 = ("J", "C", "T", "A"); my %seen = (); foreach (@Array1) { $seen{$_}++ }; my $count = 0; foreach (@Array2) { exists $seen{$_} and $count++; } print "Number of duplicates: $count\n";
Answer: Count of words that match in an array
contributed by Russ

This is not necesarily the most CPU efficient way...

# Turn at least one of the arrays into a hash (hashes are perfect for +this kind of thing) @Hash1{"A", "B", "C", "D"} = undef; # Then, just use exists... print scalar grep {exists $Hash1{$_}} ("J", "C", "T", "A");
Answer: Count of words that match in an array
contributed by BUU

grep {$x=$_;grep{$_ eq $x} @ar1} @ar2;
Answer: Count of words that match in an array
contributed by thelenm

perlfaq4 also has an answer, if the elements of each array are unique. See How do I compute the intersection of two arrays?

Answer: Count of words that match in an array
contributed by simmisam

my @Array1 = qw( A B C D ); my @Array2 = qw( J C T A ); my $count=0; for ( @Array1 ) { $count++ if $_ ~~ @Array2; } print "Count = $count\n";
Answer: Count of words that match in an array
contributed by BUU

use Benchmark; @Array1 = ("A", "B", "C", "D"); @Array2 = ("J", "C", "T", "A"); %seen = (); timethese(1_000_000, { 'stephen'=> sub { %seen=();foreach (@Array1) { $seen{$_}++ }; my $count = 0; foreach (@Array2) { exists $seen{$_} and $count++; } }, 'Russ'=> sub { @Hash1{@Array1} = undef; $matches=scalar grep {exists $Hash1{$_}} @Array2; }, 'BUU'=> sub { $matches=grep{$x=$_;grep{$_ eq $x} @Array1} @Array2; }, 'Perlfaq'=> sub { @union = @intersection = @difference = (); %count = (); foreach $element (@Array1, @Array2) {$count{$element}++ foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, +$element; } } }); __DATA__ Benchmark: timing 1000000 iterations of BUU, Perlfaq, Russ, stephen... BUU: 14 wallclock secs (15.17 usr + 0.00 sys = 5.17 CPU) @ 659 +19.58/s (n=1000000) Perlfaq: 58 wallclock secs (57.13 usr + 0.00 sys = 7.13 CPU) @ 175 +03.94/s (n=1000000) Russ: 6 wallclock secs ( 5.12 usr + 0.00 sys = 5.12 CPU) @ 195 +312.50/s (n=1000000) stephen: 17 wallclock secs (15.99 usr + 0.01 sys = 6.00 CPU) @ 625 +00.00/s (n=1000000)
Editor's Note: Corrected minor code error and re-ran results. Also, note that Perlfaq computes more than the simple intersection, so its results may be misleading at first glance. Using a hash is still the clear winner, Russ' comment notwithstanding. :-)
Answer: Count of words that match in an array
contributed by songahji

Get the intersection (on sets operation)

@Array1 = ("A", "B", "C", "D"); @Array2 = ("J", "C", "T", "A"); foreach $e (@Array1, @Array2) { $union{$e}++ && $intersect{$e}++ } @intersect = keys %intersect;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (14)
    As of 2015-07-07 15:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (90 votes), past polls