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

查看:155
本文介绍了如何从Perl中的数组中删除重复的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中有一个数组:

  my @my_array =(one,two,three ,二三); 

如何从数组中删除重复项?

解决方案

您可以按照 perlfaq4

  sub uniq {
my%seen;
grep!$ seen {$ _} ++,@_;
}

我的@array = qw(一二三二三);
我的@filtered = uniq(@array);

打印@ filtered\\\
;

输出:

 code>一二三

如果要使用模块,请尝试 uniq 函数从 列表:: MoreUtils


I have an array in Perl:

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

How do I remove the duplicates from the array?

解决方案

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";

Outputs:

one two three

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

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

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