如何从Perl中的数组中删除重复项?
时间:2020-03-05 18:38:55 来源:igfitidea点击:
我在Perl中有一个数组:
my @my_array = ("one","two","three","two","three");
如何从阵列中删除重复项?
解决方案
回答
我们可以执行以下操作,如perlfaq4所示:
sub uniq { my %seen; grep !$seen{$_}++, @_; } my @array = qw(one two three two three); my @filtered = uniq(@array); print "@filtered\n";
输出:
one two three
如果要使用模块,请尝试使用List :: MoreUtils中的Uniq函数。
回答
我通常的做法是:
my %unique = (); foreach my $item (@myarray) { $unique{$item} ++; } my @myuniquearray = keys %unique;
如果我们使用哈希并将项目添加到哈希中。我们还可以知道每个项目出现在列表中的次数。
回答
Perl文档附带了很多常见问题解答。问题经常被问到:
% perldoc -q duplicate
从上面命令的输出中回答,复制和粘贴如下所示:
Found in /usr/local/lib/perl5/5.10.0/pods/perlfaq4.pod How can I remove duplicate elements from a list or array? (contributed by brian d foy) Use a hash. When you think the words "unique" or "duplicated", think "hash keys". If you don't care about the order of the elements, you could just create the hash then extract the keys. It's not important how you create that hash: just that you use "keys" to get the unique elements. my %hash = map { $_, 1 } @array; # or a hash slice: @hash{ @array } = (); # or a foreach: $hash{$_} = 1 foreach ( @array ); my @unique = keys %hash; If you want to use a module, try the "uniq" function from "List::MoreUtils". In list context it returns the unique elements, preserving their order in the list. In scalar context, it returns the number of unique elements. use List::MoreUtils qw(uniq); my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7 my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7 You can also go through each element and skip the ones you've seen before. Use a hash to keep track. The first time the loop sees an element, that element has no key in %Seen. The "next" statement creates the key and immediately uses its value, which is "undef", so the loop continues to the "push" and increments the value for that key. The next time the loop sees that same element, its key exists in the hash and the value for that key is true (since it's not 0 or "undef"), so the next skips that iteration and the loop goes to the next element. my @unique = (); my %seen = (); foreach my $elem ( @array ) { next if $seen{ $elem }++; push @unique, $elem; } You can write this more briefly using a grep, which does the same thing. my %seen = (); my @unique = grep { ! $seen{ $_ }++ } @array;
回答
安装列表::来自CPAN的MoreUtils
然后在代码中:
use strict; use warnings; use List::MoreUtils qw(uniq); my @dup_list = qw(1 1 1 2 3 4 4); my @uniq_list = uniq(@dup_list);
回答
最后一个很好。我会稍微调整一下:
my @arr; my @uniqarr; foreach my $var ( @arr ){ if ( ! grep( /$var/, @uniqarr ) ){ push( @uniqarr, $var ); } }
我认为这可能是最易读的方法。
回答
变量@array是具有重复元素的列表
%seen=(); @unique = grep { ! $seen{$_} ++ } @array;