Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Finding unique elements in an array

by RazorbladeBidet (Friar)
on Mar 15, 2005 at 13:14 UTC ( [id://439616]=note: print w/replies, xml ) Need Help??


in reply to Finding unique elements in an array

I think that is probably the clearest way to do it. Here's a way (which I'm not 100% works 100% of the time) that operates on the original list... but as you can see, it's nowhere near as clear:
sub uniq2 { my @out = (); my ($a, $lasta); for ( my $i = 0; $i < @_; $i++ ) { splice @_, $i--, 1 if defined($lasta) && $_[$i] eq $lasta; $lasta = $_[$i]; } return @_; }
I would stick with something similar to what you have above - it's easy to read, follow and maintain.

Although I'm sure someone else can think of an uber-elegant solution :)
--------------
It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-18 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found