Bob,
On Wed, 08 Jun 2005 15:26:29 -0400
Bob <[EMAIL PROTECTED]> wrote:
> Nick Leverton wrote:
> > There's an even neater way to write uniq, which also preserves the
> > order:
> #!/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
Just try this... ;-)
#!/usr/bin/perl -w
use strict;
use Benchmark;
my @list = qw(a b c d e f g h i j k l 1 2 3 1 2 4 m n o p q r s t u v w
x y z a e f h j a z);
sub uniq_nick { my %u = (); grep { ! ($u{$_}++) } @_; }
# sub uniq_hah { grep { !(${_}{$_}++) } @_; }
sub uniq_bob { my %u = () ; for ( @_ ) { $_ and $u{$_} = 1 } ;
keys %u if ref \%u };
sub uniq_bob2 { grep { $_ ne $/ and $/ = $_ } sort grep {
$_ } @_ ; }
timethese( 1_000_000, { # better set to 10_000_000
'nick'=> 'uniq_nick(@list)',
# 'hah' => 'uniq_hah(@list)',
'bob' => 'uniq_bob(@list)',
'bob2' => 'uniq_bob2(@list)',
});
# print 'hah: '.join(",", uniq_hah(@list))."\n";
print 'bob: '.join(",", uniq_bob(@list))."\n";
print 'bob2: '.join(",", uniq_bob2(@list))."\n";
print 'nick: '.join(",", uniq_nick(@list))."\n";
__END__
Hanno