使用 Perl 检查数据数组中的重复项的最有效方法是什么? [英] What's the most efficient way to check for duplicates in an array of data using Perl?

查看:23
本文介绍了使用 Perl 检查数据数组中的重复项的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查看字符串数组中是否有重复项,最省时的方法是什么?

解决方案

我喜欢 Perl 的一个原因是它几乎可以像英语一样阅读.有点道理.

使用严格;使用警告;我的@array = qw/yes 没有可能是真的假假可能没有/;我的 %seen;foreach 我的 $string (@array) {下一个除非 $seen{$string}++;print "'$string' 重复.\n";}

输出

<块引用>

'false' 重复.

'no' 重复.

I need to see if there are duplicates in an array of strings, what's the most time-efficient way of doing it?

解决方案

One of the things I love about Perl is it's ability to almost read like English. It just sort of makes sense.

use strict;
use warnings;

my @array = qw/yes no maybe true false false perhaps no/;

my %seen;

foreach my $string (@array) {

    next unless $seen{$string}++;
    print "'$string' is duplicated.\n";
}

Output

'false' is duplicated.

'no' is duplicated.

这篇关于使用 Perl 检查数据数组中的重复项的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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