回答
這是perlfaq5 - How can I remove duplicate elements from a list or array?
只需使用散列來計算元素,然後打印只顯示一次的元素。
use strict;
use warnings;
my @array = qw(18 1 18 3 18 1 1 2 3 3);
my @nondup = do {
my %count;
$count{$_}++ for @array;
grep {$count{$_} == 1} keys %count;
};
print "@nondup\n";
輸出:
2
僅用於踢球,單線:'my @nondup =地圖鍵%{$ _-> {unique} || {}},List :: Util :: reduce {$ a - > {seen} {$ b } ++?刪除$ a - > {unique} {$ b}:undef $ a - > {unique} {$ b}; $ a} {},@array;' – ysth 2014-09-19 16:28:57
@ysth或者在荒謬性上略有變化:'my @nondup = map {my $ h = $ _; (grep {$ h - > {$ _} == 1} keys%$ h)} List :: Util :: reduce {$ a - > {$ b} ++; $ a} {},@array;' – Miller 2014-09-19 19:22:42
您還可以在簡單的方式嘗試。
use strict;
use warnings;
my @array = qw(7 8 7 5 18 1 18 3 18 1 1 2 3 3 4 5 6 7);
my $tm = "";
my %hash=();
foreach $tm(@array){
if(exists $hash{$tm}){
$hash{$tm} = "";
}
else{
$hash{$tm} = "$tm";
}
}
print join ("\n", values %hash);exit;
- 1. 在Perl中發送一個隱藏類型的數組CGI
- 2. 如何從一個數組在Perl
- 3. Coredata發現一個數組包含值
- 4. 如何在Perl中打印一個多維數組的數組?
- 5. 使兩個數組在Perl
- 6. 發現兩個數組
- 7. 的Perl - 推行到一個數組
- 8. 如何在Perl中實現數組?
- 9. 發現,並把數到一個變量用Perl
- 10. 如何在Perl中創建一個動態數組數組?
- 11. 在perl中創建一個數組數組
- 12. 在Perl數組
- 13. 發現數組中的第一個錯誤的遞歸函數
- 14. PHP - 發現如果一個數組包含一個元素
- 15. 發現在其第一個項目在一個元組匹配
- 16. 獲取一組到一個排序的數組perl
- 17. 從perl中刪除另一個數組中的一個數組中的元素
- 18. Perl數組,鍵返回一個錯誤
- 19. 發現其在數組
- 20. 從Perl中的其他數組grep一個數組
- 21. Perl:切片數組,不創建一個全新的數組
- 22. Perl - 捕獲一個數組中出現多個元素的句子
- 23. 的MongoDB /貓鼬發現兩個數組
- 24. 發現隨機元素從一個數組在PHP
- 25. Perl數組迭代錯誤 - 發現錯誤,但爲什麼?
- 26. Perl數據結構:如何從一個數組創建一個數組引用
- 27. Perl - 如何用預先存在的數組的值創建一個數組?
- 28. 如何用perl中的現有數組聲明數組?
- 29. 一個發現設置組陣列
- 30. 排序二維數組在Perl一排
請顯示您的工作 - SO不是代碼編寫服務。作爲10的入門者,我會提供:使用散列,因爲散列很適合查找唯一身份。而且 - 不要使用帶有逗號分隔值的'qw' - 這太髒了。 – Sobrique 2014-09-19 15:50:32
不要混合使用逗號和qw(通常);你的數組將包含例如「3」, – ysth 2014-09-19 15:54:33
...和「使用警告;'會警告'qw'錯誤。 – toolic 2014-09-19 15:56:11