http://www.perlmonks.org?node_id=1010720


in reply to Removing elemets from an array

Just for total completeness ;-)

#!/usr/bin/perl + use strict; use warnings; use Set::Scalar; use Data::Dumper; my @arcb = ( 450, 625, 720, 645 ); my @arca = ( 625, 645 ); my $s1 = Set::Scalar->new(@arcb); my $s2 = Set::Scalar->new(@arca); @arcb = @{ $s1 - $s2 }; print Dumper( \@arcb ); __END__ $VAR1 = [ 450, 720 ];

Regards, Karl

P.S.: No, i don't get paid for endorsement of Set::Scalar ;-) And yes, i'm repeating myself: Re: Is it possible to find the matching words and the percentage of matching words between two texts?

Update

#!/usr/bin/perl + use Benchmark qw ( :hireswallclock cmpthese timethese ); use strict; # use warnings; use Set::Scalar; use Data::Dumper; use List::Compare; our @arcb = ( 450, 625, 720, 645 ); our @arca = ( 625, 645 ); sub davido { our @arcb; our @arca; @arcb = do { my %exclude; @exclude{@arca} = (); grep { !exists $exclude{$_} } @arcb; }; # print "@arcb\n"; } sub karlgoethebier { our @arcb; our @arca; my $s1 = Set::Scalar->new(@arcb); my $s2 = Set::Scalar->new(@arca); @arcb = @{ $s1 - $s2 }; # print Dumper( \@arcb ); } sub Lotus1 { our @arcb; our @arca; my $lc = List::Compare->new( \@arca, \@arcb ); @arcb = $lc->get_Ronly; # print "arcb: @arcb\n"; } sub LanX { my ( %arcb, %arca ); our @arcb; our @arca; @arcb{@arcb} = (); @arca{@arca} = (); delete @arcb{@arca}; @arcb = keys %arcb; # print Dumper( \@arcb ); } sub linuxer { our @arcb; our @arca; my %unwanted; $unwanted{$_}++ for @arca; @arcb = grep { !$unwanted{$_} } @arcb; # print "@arcb\n"; } sub nithins { our @arcb; our @arca; foreach my $a (@arca) { for ( my $i = 0 ; $i < scalar(@arcb) ; $i++ ) { delete $arcb[$i] if ( $a == $arcb[$i] ); } } # print "@arcb"; } # karlgoethebier; # Lotus1; # LanX; # davido; # linuxer; # nithins; my $results = timethese( -10, { 'karlgoethebier' => 'karlgoethebier', 'Lotus1' => 'Lotus1', 'LanX' => 'LanX', 'davido' => 'davido', 'linuxer' => 'linuxer', 'nithins' => 'nithins', } ); cmpthese($results); __END__ Benchmark: running LanX, Lotus1, davido, karlgoethebier, linuxer, +nithins for at least 10 CPU seconds... LanX: 10.513 wallclock secs (10.51 usr + 0.00 sys = 10.51 CPU) @ +378147.76/s (n=3974333) Lotus1: 10.4951 wallclock secs (10.49 usr + 0.00 sys = 10.49 CPU) + @ 23020.02/s (n=241480) davido: 10.5748 wallclock secs (10.58 usr + 0.01 sys = 10.59 CPU) + @ 378623.51/s (n=4009623) karlgoethebier: 10.4698 wallclock secs (10.47 usr + 0.00 sys = 10 +.47 CPU) @ 9713.28/s (n=101698) linuxer: 10.4635 wallclock secs (10.46 usr + 0.00 sys = 10.46 CPU +) @ 434439.87/s (n=4544241) nithins: 10.612 wallclock secs (10.61 usr + 0.00 sys = 10.61 CPU) + @ 589604.05/s (n=6255699) Rate karlgoethebier Lotus1 LanX davido linux +er nithins karlgoethebier 9713/s -- -58% -97% -97% -9 +8% -98% Lotus1 23020/s 137% -- -94% -94% -9 +5% -96% LanX 378148/s 3793% 1543% -- -0% -1 +3% -36% davido 378624/s 3798% 1545% 0% -- -1 +3% -36% linuxer 434440/s 4373% 1787% 15% 15% +-- -26% nithins 589604/s 5970% 2461% 56% 56% 3 +6% --

"The first priority of Set::Scalar is to be a convenient interface to sets. While not designed to be slow or big, neither has it been designed to be fast or compact. Jarkko Hietaniemi <jhi@iki.fi>"

«The Crux of the Biscuit is the Apostrophe»