Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: remove duplicate value in array

by VSarkiss (Monsignor)
on Jul 11, 2006 at 13:52 UTC ( #560460=note: print w/replies, xml ) Need Help??

in reply to remove duplicate value in array

Fletch and Nevtlathiel have it above. Just in case the code sample is too dense, here's a little "looser" version that does the same thing:

my %seen; $seen{$_}++ for @content; my @unique = keys %seen;
So %seen is the hash that gets a key for each unique value in @content, and @unique is an array containing those (unique) keys.

Replies are listed 'Best First'.
Re^2: remove duplicate value in array
by ptum (Priest) on Jul 11, 2006 at 14:45 UTC

    And of course, the nice thing about this particular solution is you can still tell if there were any duplicate keys ... in such cases, the value of $seen{$key} will be greater than 1.

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re^2: remove duplicate value in array
by ikegami (Pope) on Jul 11, 2006 at 22:01 UTC
    It doesn't do quite the same thing. Fletch's solution preserves order. Yours does not.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://560460]
[Lady_Aleena]: Having to rewrite some parts of my site for mobile devices is such a pain in the butt.
[shmem]: Lady_Aleena: what editor are you using, on what platform?
[Lady_Aleena]: shmem, geany on Debian jessie.
[shmem]: some experience with vim?
[Lady_Aleena]: None
[shmem]: I don't know geany, and don't know whether it has support for ctags.
[Lady_Aleena]: ctags?
[shmem]: ctags is a program which (recursively) extracts the symbols from source and stores them in a one-file database. This allows you to query the locations where these symbols (e.g. a subroutine name) are used anywhere in the source code tree...
[shmem]: ...from inside the editor.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-04-27 12:05 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (505 votes). Check out past polls.