如何在 Perl 数组中打印唯一元素? [英] How do I print unique elements in Perl array?

查看:30
本文介绍了如何在 Perl 数组中打印唯一元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 while 语句中将元素推入数组.每个元素都是一个老师的名字.循环结束时,数组中最终会出现重复的教师姓名.有时它们在阵列中彼此不相邻,有时它们是.

I'm pushing elements into an array during a while statement. Each element is a teacher's name. There ends up being duplicate teacher names in the array when the loop finishes. Sometimes they are not right next to each other in the array, sometimes they are.

在完成将值推入数组后,如何仅打印该数组中的唯一值?每次我想打印一个元素时都不必解析整个数组.

How can I print only the unique values in that array after its finished getting values pushed into it? Without having to parse the entire array each time I want to print an element.

这是将所有内容都推入数组后的代码:

Heres the code after everything has been pushed into the array:

$faculty_len = @faculty;
$i=0;
while ($i != $faculty_len)
{
        printf $fh '"'.$faculty[$i].'"';
        $i++;
}   

推荐答案

use List::MoreUtils qw/ uniq /;
my @unique = uniq @faculty;
foreach ( @unique ) {
    print $_, "\n";
}

这篇关于如何在 Perl 数组中打印唯一元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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