[JOB] Perl Software Developer and Database programmer

Robin Vickery robinv at gmail.com
Thu Feb 23 10:27:25 GMT 2006


On 23/02/06, Peter Hickman <peter at semantico.com> wrote:
> 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;
>         }
>     }
> }
>

I'm only a beginner at perl, but I'd try something like this:

my %betas = map {($_ => [])} @alphas;
push @{$betas{$_}}, $_ for @betas;
push @results, @{$betas{$_}} for @alphas;

  -robin



More information about the london.pm mailing list