Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Count of words that match in an array

by Al Shiferaw (Initiate)
on May 10, 2002 at 02:54 UTC ( [id://165582]=perlquestion: print w/replies, xml ) Need Help??

Al Shiferaw has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

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.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Count of words that match in an array
by stephen (Priest) on May 10, 2002 at 03:05 UTC
    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";
Re: Count of words that match in an array
by Russ (Deacon) on May 10, 2002 at 03:23 UTC
    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");
Re: Count of words that match in an array
by BUU (Prior) on May 10, 2002 at 04:50 UTC
    grep {$x=$_;grep{$_ eq $x} @ar1} @ar2;
Re: Count of words that match in an array
by thelenm (Vicar) on May 10, 2002 at 16:17 UTC
Re: Count of words that match in an array
by simmisam (Novice) on Jan 15, 2014 at 07:10 UTC
    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";
Re: Count of words that match in an array
by BUU (Prior) on May 10, 2002 at 22:03 UTC
    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. :-)
Re: Count of words that match in an array
by songahji (Friar) on May 09, 2005 at 18:00 UTC
    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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://165582]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found