Hill, Greg wrote:
Can't you just do:
my %seen = ();
my @unique = grep { !$seen{$_}++ } @tree;
Actually this would be more efficient (and perlish):
my @unique = do { my %s ; @s{ @trees } = '' ; keys %s };
It would give him strings instead of hash refs, and it would lose the
order. So, it's totally wrong. It might be more efficient (which I
Yeah, that's what I said in my response to him. I also explained why he
was getting the HASH(0x..) instead of his references.
would say is debatable), but how is it more "perlish"? They're both
pretty perlish to me.
First, with the do block you don't have temp variables cluttering up
your namespace. Second, you're not iterating through the list, you're
just dumping it into the hash. Hmmm ... I feel a benchmark coming on ...
#!/usr/bin/perl -w
use strict;
use Benchmark qw( cmpthese );
my @array = 0 .. 9;
sub alan { my @unique = do { my %s ; @s{ @array } = '' ; keys %s } }
sub greg { my %s ; my @unique = grep { ! $s{ $_ }++ } @array }
cmpthese( 100000, {
'alan' => \&alan,
'greg' => \&greg,
});
Rate greg alan
greg 53763/s -- -10%
alan 59880/s 11% --
Mine is about 10% faster. Not much of a difference really.
He said it worked.
I misunderstood what he wanted to do it looks like.
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/