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

后端 未结 12 2028
花落未央
花落未央 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:41

    For numbers:

        my ($min,$max) = (sort {$a <=> $b} @array)[0,-1];
    

    For strings:

        my ($min,$max) = (sort {$a cmp $b} @array)[0,-1];
    

提交回复
热议问题