如何从 Perl 中的数组中删除重复项? [英] How do I remove duplicate items from an array in Perl?

查看:45
本文介绍了如何从 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

这篇关于如何从 Perl 中的数组中删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆