2008-08-11 15 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 

आप एक मॉड्यूल का उपयोग करना चाहते हैं, uniq समारोह से List::MoreUtils

+27

$ का उपयोग नहीं करें एक का प्रयोग करें या $ ख उदाहरण में के रूप में वे प्रकार() – szabgab

+2

के जादू वैश्विक हैं यह इस में एक 'my' शाब्दिक है गुंजाइश, तो यह ठीक है। कहा जा रहा है, संभवतः एक और वर्णनात्मक चर नाम चुना जा सकता है। – ephemient

+2

@ephemient yes, लेकिन अगर आप इस फ़ंक्शन में सॉर्टिंग जोड़ना चाहते हैं तो यह '$ :: a' और' $ :: b' ट्रम्प करेगा, है ना? – vol7ron

20

कोशिश मेरे ऐसा करने का सामान्य तरीका यह है:

यदि आप हैश का उपयोग करते हैं और आइटम को हैश में जोड़ते हैं। आपके पास यह जानने का बोनस भी है कि सूची में प्रत्येक आइटम कितनी बार दिखाई देता है।

+2

यदि आपको इसकी आवश्यकता है, तो मूल आदेश को संरक्षित करने का नकारात्मक पक्ष है। –

+0

'फोरैच' लूप के बजाय [स्लाइस] (http://perldoc.perl.org/perldata.html#Slices) का उपयोग करना बेहतर है: '@unique {@myarray} =()' – Onlyjob

115

पर्ल प्रलेखन अक्सर पूछे जाने वाले प्रश्नों के अच्छे संग्रह के साथ आता है। आपका प्रश्न अक्सर पूछे जाने वाले है:

% 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; 
+0

http: // perldoc .perl.org/perlfaq4.html # कैसे-कर-मैं-हटा-डुप्लिकेट-तत्व-से-सूची-या-सरणी% 3F – szabgab

+14

जॉन एज़ में महा एंजर्स महल प्रतिनिधि चोरी! –

+5

मुझे लगता है कि आपको वास्तव में सवाल उठाने के लिए बोनस अंक प्राप्त करना चाहिए। –

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); 
+2

यही जवाब है! लेकिन मैं केवल आपको एक बार वोट दे सकता हूं। – Axeman

+3

तथ्य यह है कि सूची :: MoreUtils को बंडल नहीं किया गया है w/perl kinda इसे उपयोग करने वाली परियोजनाओं की पोर्टेबिलिटी को नुकसान पहुंचाता है :((मैं एक के लिए नहीं) – yPhil

+3

@Ranguard: '@ dup_list'' uniq' कॉल के अंदर होना चाहिए, नहीं '@ dups' – incutonez

3

वह अंतिम व्यक्ति बहुत अच्छा था। मैं इसे थोड़ा सा ट्विक कर दूंगा:

my @arr; 
my @uniqarr; 

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

मुझे लगता है कि यह संभवतः ऐसा करने का सबसे पठनीय तरीका है।

+0

अधिक स्वतंत्र .. – laki

6

चर @array डुप्लिकेट तत्वों को

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

साथ सूची एक सरल पर्ल एक लाइनर के साथ किया जा सकता है है।

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 में

डाटा नक्शे में खिलाया जाता है। एमएपी एक अज्ञात हैश बनाता है। कुंजी हैश से निकाली जाती है और @out

0

इसे आज़माएं, ऐसा लगता है कि यूनिक फ़ंक्शन को ठीक तरह से काम करने के लिए एक क्रमबद्ध सूची की आवश्यकता है।अद्वितीय हैश चाबियों का

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); 
संबंधित मुद्दे