develooper Front page | perl.beginners | Postings from February 2009

Re: How to speed up two arrays compare.

Thread Previous | Thread Next
Marc Lucksch
February 12, 2009 00:55
Re: How to speed up two arrays compare.
Message ID:
kevin liu schrieb:
>         --------------------------------------------------------
>         But this algorithm takes a long time to compare, could you please
> help to improve this piece of
> code to less the time needed?
>         Thanks in advance.
I don't know if it's faster, but you can also use the smart-match 
operator of perl5.10.0.

use feature ':5.10';
use strict;
use warnings;

my @a=qw/A B C/;
my @b=qw/A B C/;
my @c=qw/C A B/;

say "\@a=\@b" if @a ~~ @b; # True
say "\@a=\@c" if @a ~~ @c; # Won't work this way, wrong order

my %match;
@match{@c}=(1) x @c;
say "\@a=\@c" if @a ~~ %match; # But this works.

Or if you don't have perl5.10.0

print "\@c=\@a\n" if @{[@match{@a}]} == scalar(@a) ; #Not perl5.10.0 way

Does anyone have a better idea how to force list context in the last line?

scalar(@match{@a}) returns 1, the first element, not 3

Marc "Maluku" Lucksch

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About