in reply to Printing and Removing Duplicates in Arrays
my @ary1 = (1, 2, 3, 4, 5, 6, 7); my @ary2 = (6, 7, 8, 9, 10, 11); my %h1 = map {$_ => 1} @ary1; my %h2 = map {$_ => 1} @ary2; my %dupes; @ary1 = grep {(! (exists $h1{$_} and exists $h2{$_})) or do{$dupes{$_} +=1;0}} @ary1; @ary2 = grep {(! (exists $h1{$_} and exists $h2{$_})) or do{$dupes{$_} +=1;0}} @ary2; print "ary1 is @ary1 \n"; print "ary2 is @ary2 \n"; my @dupes = keys %dupes; print "dupes were @dupes \n";
Hope that helps,
andye.
Edited to make dupes a hash rather than an array, to prevent dupes in the dupes!
Another minor edit to prevent a warning
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Printing and Removing Duplicates in Arrays
by jmcnamara (Monsignor) on Aug 21, 2002 at 11:04 UTC | |
by andye (Curate) on Aug 21, 2002 at 11:14 UTC |