Perl: Find unique items in an array
I need to extract all the unique elements from an array. There is no built-in way to do this, but there are several user functions you can use.
my @x = qw(one two one three one four);
my @y = array_unique(@x); # ("one", "two", "three", "four")
# Borrowed from: https://perlmaven.com/unique-values-in-an-array-in-perl
sub array_unique {
my %seen;
return grep { !$seen{$_}++ } @_;
}
I stand corrected, List::Util
includes a uniq()
function to do exactly this, is a core module, and is included with all Perl installations.