Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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);

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 pondering the Monastery: (3)
As of 2016-10-27 02:49 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (352 votes). Check out past polls.