Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Removing Duplicate Array Elements!!!

by tachyon (Chancellor)
on Sep 27, 2001 at 17:33 UTC ( #115087=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Re: Removing Duplicate Array Elements!!!
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Re: Removing Duplicate Array Elements!!!
by gbarr (Monk) on Sep 27, 2001 at 19:45 UTC
    If you are looking an efficient method to do this then try.

    my %hash;
    @hash{@a} = ();
    @a = keys %hash;
    

    But that only works, just as your example, for non-references as the keys of a hash can only be strings. If @a may contain references that you want to preserve then use

    my %hash;
    @hash{@a} = @a;
    @a = values %hash;
    

    This is slightly less efficient as it causes an extra copy of each element in @a

    I generally write this as

    @a = do { my %h; @h{@a} = @a; values %h }; # unique
    

      As you say, the slice method is faster again. I will be using it from now on. Thanks.

      use Benchmark; @a= (0..100,0..100); $mine = <<'CODE'; my %hash; @hash{$_}++ for @a; @a = keys %hash; CODE $yours1 = <<'CODE'; my %hash; @hash{@a} = (); @a = keys %hash; CODE $yours2 = <<'CODE'; my %hash; @hash{@a} = @a; @a = values %hash; CODE $his = <<'CODE'; my %hash; map { $hash{$_}=1 } @a; @a = keys %hash; CODE timethese ( 10000, { 'mine' => $mine, 'yours1' => $yours1, 'yours2' => $yours2, 'his' => $his } ); __END__ C:\>perl test.pl Benchmark: timing 10000 iterations of his, mine, yours1, yours2... his: 17 wallclock secs (17.03 usr + 0.00 sys = 17.03 CPU) @ 58 +7.20/s (n=10000) mine: 15 wallclock secs (15.05 usr + 0.00 sys = 15.05 CPU) @ 66 +4.45/s (n=10000) yours1: 12 wallclock secs (11.76 usr + 0.00 sys = 11.76 CPU) @ 85 +0.34/s (n=10000) yours2: 15 wallclock secs (14.78 usr + 0.00 sys = 14.78 CPU) @ 67 +6.59/s (n=10000) C:\>

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Hi thank u so much for your code ..it is working it has solved my problem ..but as a beginner i want know wats happening there ..could plz explain me the code..thanks in advance.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://115087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2015-07-29 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (267 votes), past polls