Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: removing repeated elements from one array

by Intrepid (Deacon)
on Dec 15, 2005 at 23:13 UTC ( #517103=note: print w/ replies, xml ) Need Help??

in reply to Re: removing repeated elements from one array
in thread removing repeated elements from one array

g0n cited perlfaq4 thusly:

If you don't care about the order of the elements, you could just create the hash then extract the keys. It's not important how you create that hash: just that you use keys to get the unique elements.

If you do care about the ordering of the input list data, and the data are simple scalars (typically strings), then what you want is sometimes termed idempotency: you want a value to appear only once, the first time is meaningful, but any occurences after the first appearance need to be removed. I asked about this some time ago and (to reiterate the final results of the discussion that took place there), we came up with the following technique:

my %seen =(); @unique_array = grep(! $seen{$_}++ , @non_unique_array);

Comment on Re^2: removing repeated elements from one array
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-12-01 08:00 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (796 votes), past polls