如何打印在Perl阵列独特的元素? [英] How do I print unique elements in Perl array?

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

问题描述

我在while语句中的元素推到一个数组。每个元素都是一个老师的名字。这里结束了在阵列中被重复老师的名字时,循环结束。有时它们不是正确的阵列中彼此相邻,有时他们是

我怎样才能数组中只打印唯一值后的成品获得价值推到了吗?无需每次解析整个阵列我想打印一个元素。

继承人的一切后,code已被推入阵:

  $ faculty_len = @faculty;
$ I = 0;
而($ I!= $ faculty_len)
{
    printf的$跳频'$教员[$ i]。'';
    $ I ++;
}


解决方案

 使用list :: MoreUtils QW / uniq的/;
我= @unique uniq的@faculty;
的foreach(@unique){
    打印$ _,\\ n;
}

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天全站免登陆