Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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."

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://599343]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-06-24 22:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.