develooper Front page | perl.beginners | Postings from July 2001

Re: Deleting Duplicate Lines one-liner

From:
Jeff 'japhy/Marillion' Pinyan
Date:
July 30, 2001 11:18
Subject:
Re: Deleting Duplicate Lines one-liner
Message ID:
Pine.GSO.4.21.0107301416060.28213-100000@crusoe.crusoe.net
On Jul 30, Paul said:

>> while (<INPUTFILE>){
>>    if (not $seen{$_}) {
>>        $seen{$_} = 1;
>>         print OUTFILE;
>>     }
>>     else {
>>     }
>>   }

Here's a one-liner:

  perl -nle 'print if !$seen{$_}++'

and here's another:

  perl -pe '$_ x= !$seen{$_}++'  (attributed to some of Larry's genius)

and another, for use in a program

  $seen{$_} ||= print OUT while <IN>;

Have fun. :)

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About