Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-06-20 00:14 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.