Alex Rivera | Logout

How do I remove duplicate items from an array in Perl?

Asked 2008-08-11T10:04:32.093
177

I have an array in Perl:

my @my_array = ("one","two","three","two","three");

How do I remove the duplicates from the array?

Edit
Report

1 Answer

11

Can be done with a simple Perl one-liner.

my @in=qw(1 3 4  6 2 4  3 2 6  3 2 3 4 4 3 2 5 5 32 3); #Sample data 
my @out=keys %{{ map{$_=>1}@in}}; # Perform PFM
print join ' ', sort{$a<=>$b} @out;# Print data back out sorted and in order.

The PFM block does this:

Data in @in is fed into map. map builds an anonymous hash. keys are extracted from the hash and feed into @out

answered 2011-11-09T21:23:25.667

Your Answer