Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 exploiting the Monastery: (10)
As of 2014-12-18 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (49 votes), past polls