2012-07-24 145 views

回答

0
my %hash = map { $_ => 1 } @b; 

while(my $el = shift @a) 
{ 
    print $el unless defined $hash{ $el }; 
    $hash{ $el } = 0;   
} 

foreach(keys %hash) 
{ 
    print $_ if $hash{ $_ } == 1; 
} 

编辑:改变$ _到$ E1和在同时

0
use strict; 
use warnings; 

my @a=qw(one two three four five six); 
my @b=qw(zero one two three four seven); 
my %seen; 
foreach my $a (@a) { 
    $seen{$a} = 1; 
} 

foreach my $b (@b) { 
    if (defined $seen{$b} and $seen{$b} == 1) { 
    $seen{$b} = -1; 
    } else { 
    $seen{$b} = 2; 
    } 
} 

foreach my $k (keys %seen) { 
    print "$k\n" if $seen{$k} != -1; 
} 
+0

谢谢亲爱的。它可以通过此程序解决我的问题删除 '打印'。 – CHE 2012-07-24 11:26:40

1
#!/usr/bin/perl 

@a=qw(one two three four five six); 
@b=qw(zero one two three four seven); 

$words{$_}++ for (@a, @b); 
while (($k, $v) = each %words) { 
    next if $v > 1; 
    print "$k "; 
} 
0
my %hash; $hash{$_}++ for @a,@b; 
say join " ", grep { $hash{$_} == 1 } keys %hash;