identifying the duplicate when removing duplicates from any array

J

Jack

Hello,

The following removes duplicates from my array, however I cannot figure
out how to get Perl to also output the duplicate offender itself, since
I want to know "which" was the actual duplicate it removed on the
output... knowing what below is the duplicate is simple by looking at
the array (t1), but imagine a huge array, and you want to know "which"
were the dups after processing, what would I need to add to the code
below to output the actual duplicate being t1 ? Any tips are
appreciated :

@in = ("t1", "t2", "t1", "t3");
# Sort First, then Dedup
@in = sort @in;
# Start Dedup routine into @out array
print " IN ", $#in;
$prev = "not equal to $in[0]";
@out = grep($_ ne $prev && ($prev = $_, 1), @in);
print " OUT ", $#out;

Thank you !
Jack
 
T

Tad McClellan

Jack said:
The following removes duplicates from my array, however I cannot figure
out how to get Perl to also output the duplicate offender itself,


-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my @in = ("t1", "t2", "t1", "t3", 't1', 't2', 't2');

my %cnt;
foreach ( @in ) {
print "$_\n" if $cnt{$_}++;
}
my @out = keys %cnt;
-------------------------------

Thank you !


Uh huh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top