如何使用 perl 输出唯一、计数和总和 [英] How can I output unique, count and sum using perl

查看:36
本文介绍了如何使用 perl 输出唯一、计数和总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Perl 的新手.我需要找到以下数据的唯一值、计数和总和.我请求你的帮助.

I am new to Perl. I need to find the unique, count and sum for the following data. I request your help.

08/2009   I-111   300
08/2009   I-112   400
08/2009   I-113   500
10/2009   I-200   1000
10/2009   I-300   500
11/2009   I-300   100
11/2009   I-100   400

所以我需要找到这样的

08/2009 3 1200
10/2009 2 1500

推荐答案

uniq from List::MoreUtils 返回列表的唯一元素:

uniq from List::MoreUtils returns the unique elements of a list:

use List::MoreUtils 'uniq';
my @x = uniq 1, 1, 2, 2, 3, 5, 3, 4;    # (1,2,3,5,4)

标量上下文中的列表表达式返回元素的数量.

A list expression in scalar context returns the number of elements.

my @array = (5,6,7,8);
my $a_count = @array;       # 4

my %hash = ('x' => 1, 'y' => 2);
my $h_count = keys %hash;   # 2

sum 来自 List::Util 添加列表的元素.

sum from List::Util adds the elements of a list.

use List::Util 'sum';
my @array = (1,2,3,4,5);
print sum @array;   # 15

这篇关于如何使用 perl 输出唯一、计数和总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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