Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: dup values

by strat (Canon)
on Feb 10, 2007 at 10:29 UTC ( [id://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"

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://599343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found