perl 怎样合并两个数组,并除去相同的元素?

my @array1 = ('aa', 'bb');
my @array2 = ('cc', 'aa');

怎样得到 @array3 = ('aa', 'bb', 'cc') ???

青春有我
浏览 1427回答 2
2回答

长风秋雁

my %h = ();++$h{$_} foreach ( @array1, @array2 );my @array3 = sort keys %h;

神不在的星期二

my @array1 = ('aa', 'bb');my @array2 = ('cc', 'aa');@array3 = (@array1, @array2);my %count;my @array3_new = grep { ++$count{ $_ } < 2; } @array3;print @array3_new;或者@array3 = (@array1, @array2);foreach my $item ( @array3 ){if ( ! grep( /$item/, @array3_new ) ){push( @array3_new, $item );}}print @array3_new;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP