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

finding the distinct elements in an array

by beginr (Novice)
on May 29, 2006 at 13:04 UTC ( [id://552303]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on finding the distinct elements in an array

Replies are listed 'Best First'.
Re: finding the distinct elements in an array
by prasadbabu (Prior) on May 29, 2006 at 13:10 UTC
Re: finding the distinct elements in an array
by rafl (Friar) on May 29, 2006 at 13:10 UTC

    For example using that code:

    my %seen; my @distinct_elems = grep { !$seen{$_}++ } @array;

    or maybe

    my %seen = map { $_ => 1 } @array; my @distinct_elems = keys %seen;

    The first example even gives you the number of occurence of each element.

    Cheers, Flo

Re: finding the distinct elements in an array
by McDarren (Abbot) on May 29, 2006 at 13:09 UTC
    You know - you'd almost reckon that a question like this would be so common that it would be answered in a FAQ, wouldn't you?

    Well, guess what? ...it is.

Re: finding the distinct elements in an array
by tcf03 (Deacon) on May 29, 2006 at 14:37 UTC
    An easier to understand (for a beginner ) example might be:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; my @array = ( 1, 2, 3, 4, 5, 1, 2, 3, 99 ); my %seen = (); my @unique_elements = (); foreach my $element (@array) { push (@unique_elements, $element) unless $seen{$element}++; } print Dumper @array; print Dumper @unique_elements;
    the %seen hash relies on the autovivification feature of perl to initialize $seen{$element} when its seen. $seen{element}++ is just saying that its seen more than once... Doing a print Dumper %seen may help you.

    But as the others have said - reading the documentation is key. Data::Dumper is a great module for viewing data structures. Also diagnostics can also be helpful. Good luck.

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: finding the distinct elements in an array
by davido (Cardinal) on May 29, 2006 at 14:50 UTC

    While it's a fairly common idiom to produce, and the usual manner is to use a hash to filter uniques, it's less error-prone to simply use the common module, List::MoreUtils, with its uniq() method.

    use strict; use warnings; use List::MoreUtils qw( uniq ); my @array = (1, 1, 3, 2, 8, 9, 4, 2, 3); print uniq( @array ), "\n";

    Dave

      Which.. guess what..
      sub uniq (@) { my %h; map { $h{$_}++ == 0 ? $_ : () } @_; }
      uses a hash!

      Whilst in principal using a module is often a good thing, maybe it would be better if the OP understood what was going on under the hood first?

      Just my not so humble opinion.

      jdtoronto

Re: finding the distinct elements in an array
by ioannis (Abbot) on May 29, 2006 at 14:35 UTC
    my @a = ( 1..3, 2..3, 6, 8..9); # Extract unique elements print { @@{@a}=$,=' '; \*STDOUT} keys(%@);
Re: finding the distinct elements in an array
by johndageek (Hermit) on May 29, 2006 at 22:11 UTC
    ## manual solution
    my @a = ( 1..3, 2..3, 6, 8..9); my $last_element = ""; my @sorted_aray = sort(@a); foreach $i (@aorted_array){ if ($last_element ne $i){ print $i; $last_element = $i; } }

    Enjoy!
    Dageek

Log In?
Username:
Password:

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

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

    No recent polls found