Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-07-26 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls