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

Replies are listed 'Best First'.
Re: dup values
by strat (Canon) on Feb 10, 2007 at 10:29 UTC
    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"

      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
Re: dup values
by virtualsue (Vicar) on Feb 10, 2007 at 10:43 UTC
    You should read perldoc perlfaq4.