Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Unique elements in array

by megaurav2002 (Monk)
on May 09, 2007 at 08:47 UTC ( [id://614322]=perlquestion: print w/replies, xml ) Need Help??

megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I have a array which contains duplicate elements. I want only unique elements in the array. I just want to know does Perl has some inbuilt functionality for it or should i make comparisons while storing the values in the array. Thanks a lot!

Replies are listed 'Best First'.
Re: Unique elements in array
by borisz (Canon) on May 09, 2007 at 09:35 UTC
    use List::MoreUtils 'uniq'; my @new = uniq ( @old );
    Boris
Re: Unique elements in array
by shigetsu (Hermit) on May 09, 2007 at 08:58 UTC
    Something along the lines
    my @set = qw(a a b c c); my %seen; $seen{$_}++ foreach @set; my @unique = grep { $seen{$_} == 1 } @set;
    maybe?
      Your code could be condensed to

      my @set = qw(a a b c c); my %seen; my @unique = grep { not $seen{$_} ++ } @set;

      That method preserves the order of the original array but if that's not important you could also do

      my @set = qw(a a b c c); my %seen; @seen{@set} = (); my @unique = keys %seen;

      I hope this is of interest.

      Cheers,

      JohnGG

Re: Unique elements in array
by neniro (Priest) on May 09, 2007 at 09:20 UTC
Re: Unique elements in array
by naikonta (Curate) on May 09, 2007 at 10:04 UTC
    Hi megaurav2002, Array in perlfaq has many solutions for you. If you want to roll your own start with grep. Comeback again after you code something but it doesn't work as you expect.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Unique elements in array
by fenLisesi (Priest) on May 09, 2007 at 09:47 UTC
Re: Unique elements in array
by wjw (Priest) on May 10, 2007 at 00:37 UTC
    I love brute force... lol

    I would pump my array into a hash, then let it flow back out to an array if that was what I really wanted. I assume of course that your not banging on performance issues really hard. Or just do it the easy way and use the hash in the first place... :-)

    • ...the majority is always wrong, and always the last to know about it...
    • The Spice must flow...
    • ..by my will, and by will alone.. I set my mind in motion
Re: Unique elements in array
by Moron (Curate) on May 09, 2007 at 09:23 UTC
    It's better if you can avoid an unnecessary intermediary array, e.g. in a typical case of reading ids from the first column in a tsv file, I'd go for something like:
    my %unique; my @ids = map { chop; my @fld = split /\t/; !defined( $unique{ $fld[0] } ) && $unique{ $fld[0] }++ && $fld[0]; } <$fileHandle>;
    __________________________________________________________________________________

    ^M Free your mind!

      Ouch. chop is almost always wrong (what if your EOL marker is more than one character)--and what if $fld[0] is a valid value that evaluates to false in boolean context?

        The popular misuse of chomp() deserves a new topic. As for $fld[0] - defined( unique{ $fld[0] } ) will be true in the case you cite.

        There might be functional issues or data quality issues in any real example but these are in general OT.

        __________________________________________________________________________________

        ^M Free your mind!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://614322]
Approved by shigetsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-24 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found