Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Re: Re: Removing Duplicate Array Elements!!!

by gbarr (Monk)
on Sep 27, 2001 at 19:45 UTC ( #115125=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Removing Duplicate Array Elements!!!
in thread Removing Duplicate Array Elements!!!

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Removing Duplicate Array Elements!!!
by tachyon (Chancellor) on Sep 27, 2001 at 20:36 UTC

    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 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:\>




Re^4: Removing Duplicate Array Elements!!!
by littlemonk (Sexton) on Feb 03, 2011 at 10:22 UTC
    Hi thank u so much for your code 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://115125]
[Corion]: ambrus: I think the event loop is not really documented but implicit in X11 or Windows...
[ambrus]: One more obvious possible workaround is to just have the GUI of your program freeze for as long as the HTTP is running;
[Corion]: brb
[ambrus]: or, if Prima supports this, embedding its event loop to an outer main event loop, and running AnyEvent::HTTP in that outer one.
[ambrus]: Corion: I think there's already some form of AnyEvent integration for Prima, according to https://metacpan. org/source/ KARASIK/Prima-1. 49/examples/
[ambrus]: Corion: even better, https://metacpan. org/pod/distributi on/Prima/pod/ Prima/faq.pod#How- do-I-use-Prima- with-AnyEvent
[ambrus]: a FAQ question
[ambrus]: ah, apparently that's the POE thing you were talking about
[ambrus]: Ok, but if there's a POE integration, then I again suggest that you consider using IO::Async::Loop:: Poe . I don't know if that will work.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-12-07 16:17 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (130 votes). Check out past polls.