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

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

推荐答案

您可以做这样的事情在<一个证明href=\"http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f\">perlfaq4:

You can do something like this as demonstrated in 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的函数=htt​​ps://metacpan.org/pod/List%3a%3aMoreUtils > 列表:: MoreUtils

If you want to use a module, try the uniq function from List::MoreUtils

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

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