如何从 Perl 中的数组中删除重复项?
问题描述:
我在 Perl 中有一个数组:
I have an array in Perl:
my @my_array = ("one","two","three","two","three");
如何从数组中删除重复项?
How do I remove the duplicates from the array?
答
你可以像 perlfaq4:
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
}
my @array = qw(one two three two three);
my @filtered = uniq(@array);
print "@filtered\n";
输出:
one two three
如果您想使用模块,请尝试 uniq 功能code>List::MoreUtils
If you want to use a module, try the uniq
function from List::MoreUtils