How to find maximum and minimum value in an array of integers in Perl?

后端 未结 12 2076
花落未央
花落未央 2020-12-14 18:43

I have an array with values 33, 32, 8, 100.

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

Do I need to include any special libraries?<

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 19:40

    List::Util's min and max are fine,

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

    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 is part of core, but List::MoreUtils isn't.

提交回复
热议问题