如何在Perl中找到整数数组中的最大值和最小值? [英] How to find maximum and minimum value in an array of integers in Perl?

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

问题描述

我有一个值为 33、32、8、100 的数组.

如何在此数组中找到最大值和最小值?

How can I find the maximum and minimum value in this array?

我需要包括任何特殊的库吗?

Do I need to include any special libraries?

推荐答案

List :: Util minmax很好,

use List::Util qw( min max );
my $min = min @numbers;
my $max = max @numbers;

但是 List :: MoreUtils minmax在您使用时效率更高同时需要最小值和最大值(因为它进行的比较较少).

But List::MoreUtils's minmax is more efficient when you need both the min and the max (because it does fewer comparisons).

use List::MoreUtils qw( minmax );
my ($min, $max) = minmax @numbers;

List :: Util是核心的一部分,但List :: MoreUtils不是核心的一部分.

List::Util is part of core, but List::MoreUtils isn't.

这篇关于如何在Perl中找到整数数组中的最大值和最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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