Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: dup values

by strat (Canon)
on Feb 10, 2007 at 10:29 UTC ( #599343=note: print w/ replies, xml ) Need Help??


in reply to dup values

my %seen = (); # use hash for counting elements my @doubles = grep { # if value not in seen, return 0 and add it # to %seen with start value of 1 $seen{$_}++ } @array1; foreach my $element ( @doubles ) { print "$element\n"; } # foreach @doubles undef %seen;

disadvantage: if you have three or more values, they will be printed more than once, so the following solution might be more precise:

my %seen = (); $seen{$_}++ for @array1; for my $value ( keys %seen ) { print "$value\n" if $seen{$value} > 1; } # for %seen

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"


Comment on Re: dup values
Select or Download Code
Replies are listed 'Best First'.
Re^2: dup values
by fenLisesi (Priest) on Feb 10, 2007 at 10:57 UTC
    A variation:
    use strict; use warnings; my @critters = qw( ant camel snake camel llama cow sheep herring camel llama ); my %rec_of; my %dup_rec_of; for my $critter (@critters) { $dup_rec_of{ $critter }++ if $rec_of{ $critter }++; } printf "%s\n", join ', ', sort keys %dup_rec_of;
    which prints:
    camel, llama
    Cheers

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2015-07-08 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls