2008-08-11 501 views

回答

145

爲證明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

+27

請不要使用$ a或$ b在例子,因爲它們的sort() – szabgab 2008-09-17 07:50:06

+2

神奇全局這是一個在這一個'my`詞彙範圍,所以沒關係。這就是說,可能會選擇一個更具描述性的變量名稱。 – ephemient 2010-01-18 17:51:13

+2

@ephemient是的,但如果你要在這個函數中添加排序,那麼它會勝過`$ :: a`和`$ :: b`,不是嗎? – vol7ron 2012-02-21 16:45:38

20

嘗試uniq功能我通常的做法是:

my %unique =(); 
foreach my $item (@myarray) 
{ 
    $unique{$item} ++; 
} 
my @myuniquearray = keys %unique; 

如果您使用散列並將項添加到散列。你也有獎勵,知道每個項目出現在列表中的次數。

115

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; 
63

從CPAN

在你的代碼安裝 List::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); 
3

最後一個很不錯。我只是稍微調整它:

my @arr; 
my @uniqarr; 

foreach my $var (@arr){ 
    if (! grep(/$var/, @uniqarr)){ 
    push(@uniqarr, $var); 
    } 
} 

我認爲這可能是最可讀的方式來做到這一點。

6

變量@array是列表中有重複的元素

%seen=(); 
@unique = grep { ! $seen{$_} ++ } @array; 
5

可以用一個簡單的Perl一個襯墊來完成。

my @in=qw(1 3 4 6 2 4 3 2 6 3 2 3 4 4 3 2 5 5 32 3); #Sample data 
my @out=keys %{{ map{$_=>1}@in}}; # Perform PFM 
print join ' ', sort{$a<=>$b} @out;# Print data back out sorted and in order. 

的PFM塊執行此:

在@In

數據被饋送到MAP。 MAP建立一個匿名散列。密鑰從哈希中提取並送入@out

0

試試這個,似乎uniq函數需要一個排序列表才能正常工作。

use strict; 

# Helper function to remove duplicates in a list. 
sub uniq { 
    my %seen; 
    grep !$seen{$_}++, @_; 
} 

my @teststrings = ("one", "two", "three", "one"); 

my @filtered = uniq @teststrings; 
print "uniq: @filtered\n"; 
my @sorted = sort @teststrings; 
print "sort: @sorted\n"; 
my @sortedfiltered = uniq sort @teststrings; 
print "uniq sort : @sortedfiltered\n"; 
1

使用唯一散列鍵的概念:

my @array = ("a","b","c","b","a","d","c","a","d"); 
my %hash = map { $_ => 1 } @array; 
my @unique = keys %hash; 
print "@unique","\n"; 

輸出: ACBD

1

方法1:使用散列

邏輯:哈希只能有唯一的密鑰,所以迭代數組,將任何值分配給數組的每個元素,將元素保留爲該散列的關鍵字。返回散列鍵,它是你的唯一數組。

my @unique = keys {map {$_ => 1} @array}; 

方法2:方法1的擴展可重用性

更好地使一個子程序,如果我們都應該在我們的代碼中多次使用該功能。

sub get_unique { 
    my %seen; 
    grep !$seen{$_}++, @_; 
} 
my @unique = get_unique(@array); 

方法3:使用模塊List::MoreUtils

use List::MoreUtils qw(uniq); 
my @unique = uniq(@array);