Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Compare arrays remove identical

by perlmonkey (Hermit)
on May 02, 2001 at 11:26 UTC ( #77256=note: print w/replies, xml ) Need Help??

in reply to Compare arrays remove identical

Theres a section in Mastering Algorithms with Perl on sets also. If you need a full implementation of unions/differences/intersections etc see the Set::Scalar module. If you use chromatic's code linked to above, I gave into my compulsion to do a tiny bit of benchmarking:
my @kansas = qw( corn wheat hay cattle ); my @oklahoma = qw( wheat dairy cattle ); use Benchmark; timethese(100000, { "diff1" => sub { diff1(\@kansas, \@oklahoma) }, "diff2" => sub { diff2(\@kansas, \@oklahoma) }, }); sub diff1 { my %hash; @hash{@{$_[1]}} = (1) x @{$_[1]}; return grep { !defined $hash{$_} } @{$_[0]}; } sub diff2 { my %hash = map{ $_=>1} @{$_[1]}; return grep { !defined $hash{$_} } @{$_[0]}; }
It seems that a hash slice is a teeny bit faster than map for this purpose:
Benchmark: timing 100000 iterations of diff1, diff2... diff1: 2 wallclock secs ( 1.74 usr + 0.01 sys = 1.75 CPU) diff2: 3 wallclock secs ( 2.22 usr + 0.01 sys = 2.23 CPU)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://77256]
[1nickt]: (Disicpulus The new best choice is maybe "First People"
[karlgoethebier]: Discipulus: This is called "Volksverhetzung" in German which means
[karlgoethebier]: incitement of the people and it is prohibited by law
[1nickt]: The best sign I have sign in a protest march was carried by a kid of about 10 years "If you build a wall my generation will tear it down."
[1nickt]: ... sign I have seen ...
[1nickt]: Now that one made me hopeful! First time not angry in a long time when I saw that kid.
[Discipulus]: i propend for removing: why? because we are so few that we must find i minimal common divisor, this is certainly Perl not our (anyway private) thougths. And i say this still wondering because i love a lot freedom of expression. And i say this not for roho
[1nickt]: Discipulus that was the point of my story of taking the sticker off my truck. I know there are lots of people in the world who if I knw their private beliefs I might want to argue with them. And they with me. But life cannot all be arguments!
[1nickt]: This is less than perfect ... but demanding perfection (from people or from life) is a sure way to unhappiness.
[Discipulus]: and anyway we have CB where every (democratic) opinion can be expressed

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-06-22 12:16 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.