Dan Otterburn <> wrote:
> On Fri, 24 Aug 2007 02:42:03 +0200, Gunnar Hjalmarsson wrote:
>
>> Use a hash.
>>
>> my ( @fruits_deduped, %seen );
>> while ( my $fruit = shift @fruits ) {
>> push @fruits_deduped, $fruit unless $seen{$fruit}++;
>> }
>
> Many thanks. Just to clarify my understanding: this works because
> "unless" binds tighter than "++"
^^^^^^^^^^^^^
"unless" is not an operator, so talking about its precedence makes
no sense.
> each different $fruit - isn't auto-vivified until *after* "unless" has
> tested?
That part is accurate though.
> i.e. it is short-hand for:
>
> while ( my $fruit = shift @fruits ) {
> if ( !$seen{$fruit} ) {
> push @fruits_deduped, $fruit;
> $seen{$fruit} += 1;
> }
> }
>
>> See also "perldoc -q duplicate".
....then do it with a grep():
my %seen;
@fruits = grep !$seen{$_}++, @fruits;
And it even reads kind of Englishy "grep not seen fruits"
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"