Page 1 of 1
Perl - Removing duplicates from an array
Posted: Wed Aug 27, 2003 11:17 am
by JayBird
I have an array called final_array.
The elements of the array are read from a text file.
How can i iterate through the array to remove duplicates?
Cheerz
Mark
Posted: Wed Aug 27, 2003 11:32 am
by JayBird
if anyone is interested, thi is how you do it
Code: Select all
undef %saw;
@final_array = grep(!$saw{$_}++, @final_array);
Posted: Wed Aug 27, 2003 11:34 am
by volka
Code: Select all
my @arr = ('a', 'b', 'c', 'b', 'b', 'b', 'd', 'a', 'e' );
# here's the trick
@arr = do { my %seen; grep !$seen{$_}++, @arr };
foreach my $element (@arr) {
print $element, "\n";
}
it's not my idea, you will find it anywhere on the web

Posted: Wed Aug 27, 2003 11:42 am
by JAM
if it weren't about perl
GAH! Edited because of stupidity.
Thanks volka. I was so thinking php here...
Posted: Wed Aug 27, 2003 11:48 am
by volka
nice idea...
if it weren't about perl
