develooper Front page | perl.qpsmtpd | Postings from June 2005

Re: uniq in perl

Thread Previous | Thread Next
From:
Bob
Date:
June 8, 2005 12:19
Subject:
Re: uniq in perl
Message ID:
42A74665.8040809@nishanet.com
Nick Leverton wrote:
> On Tue, Jun 07, 2005 at 07:33:27PM -0400, Bob wrote:
>   
>> ...like to fix...allowing duplicate recipients
>>
>> sub _uniq {
>> my %u = () ; for ( @_ ) { $_ and $u{$_} = 1 } ; keys %u if ref \%u ;
>> }
>>     
>
> There's an even neater way to write uniq, which also preserves the order:
>
> my %u = (); grep { ! ($u{$_}++) } @_;
>
> List operators are so useful !  A better perl bod than me could probably
> avoid the named hash too :)
>
> Nick
Bob says:

#!/usr/bin/perl -w
use strict; # strict and warn fail a lot of anon hash tricks, so...
> sub uniq {
>  grep { $_ ne $/ and $/ = $_ } sort grep { $_ } @_ ;
> } # no named hash



Thread Previous | Thread Next


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