in reply to Re: Removing Duplicate Array Elements!!!
in thread Removing Duplicate Array Elements!!!
While using map certainly works, using map in a void context like you do is inefficient. Map returns an array for you regardless of whether you use it or not. As a result using map as a surrogate for loop is bad practice. In golf you use it because it is 2 chars shorter than a for loop. That is about the only (valid) reason to use it in a void context :-) All you actually want is:
$help{$_}++ for @a
This is far more efficient, it is also shorter for what it's worth. Thanks to chipmunk for straigtening me out on this some months ago.
my @a= (1,2,3,4,5,6,6,5,4,3,2,1); my %help; my @wasted_array = map { $help{$_}=1 } @a; @a = keys %help; print @wasted_array, "\n", @a;
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Removing Duplicate Array Elements!!!
by gbarr (Monk) on Sep 27, 2001 at 19:45 UTC | |
by tachyon (Chancellor) on Sep 27, 2001 at 20:36 UTC | |
by littlemonk (Sexton) on Feb 03, 2011 at 10:22 UTC |
In Section
Seekers of Perl Wisdom