[JOB] Perl Software Developer and Database programmer
Peter Hickman
peter at semantico.com
Thu Feb 23 09:37:15 GMT 2006
Problem is that your code does not handle duplicate values in the alpha
or beta lists. Compare
#!/usr/bin/perl -w
use strict;
use warnings;
my @alphas = qw/1 2 3 4 5 2/;
my @betas = qw/1 2 3 1 2 3/;
my @results;
foreach my $alpha ( @alphas ) {
foreach my $beta ( @betas ) {
if ( $alpha == $beta ) {
push @results, $beta;
}
}
}
print join ( ' ', @results ) . "\n";
Against
#!/usr/bin/perl -w
use strict;
use warnings;
my @alphas = qw/1 2 3 4 5 2/;
my @betas = qw/1 2 3 1 2 3/;
my %beta;
@beta{@betas} = ();
my @results = grep {exists $beta{$_}} @alphas;
print join(',', @results), "\n";
The original produces 1 1 2 2 3 3 2 2, yours produces 1,2,3,2
More information about the london.pm
mailing list