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

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?



Solution 1:[1]

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.

Solution 2:[2]

Without modules:

#!/usr/bin/perl
use strict;
use warnings;
my @array = sort { $a <=> $b } qw(33 32 8 100);
print "min: $array[0]\n";
print "max: $array[-1]\n";

Solution 3:[3]

You can use List::Util to do this easily, eg.

use List::Util qw(min max);
my @arr = (33, 32, 8, 100);
print min(@arr)," ", max(@arr), "\n";

Solution 4:[4]

The provided solutions are good, but if you want to implement it yourself it's pretty straightforward:

use strict;
use warnings;

my @array = (33, 32, 8, 100);
my ($min, $max);

for (@array) {
    $min = $_ if !$min || $_ < $min;
    $max = $_ if !$max || $_ > $max
};

print "min: $min\n";
print "max: $max\n";

Solution 5:[5]

For numbers:

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

For strings:

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

Solution 6:[6]

You can use map to do this without needing libraries:

my @array = (33, 32, 8, 100);
my ($max,$min)=(-1e99,1e99); # Initialize to values outside anything in your list
map {$max=$_ if ($_>$max); $min=$_ if($_<$min);} @array;
print "max=$max, min=$min\n";

Solution 7:[7]

You should use List::Util which has been released with the Perl distribution since v5.7.3 so probably doesn't need installing.

use strict;
use warnings;

use feature 'say';

use List::Util qw/ max min /;

my @data = (33, 32, 8, 100);

say min @data;
say max @data;

output

8
100

Solution 8:[8]

Ofcourse, if you want both the maxium and minimum value of a list at the same time, it is more efficient to fetch both at once; it only has to perform 3 order comparisons per 2 items of data, rather than 4. This may matter if the data sets are big enough.

List::Util doesn't provide a minmax function but List::MoreUtils does.

use strict;
use warnings;
use feature qw( say );

use List::MoreUtils qw( minmax );

my ( $min, $max ) = minmax @data;

say $min;
say $max;

Solution 9:[9]

I think List::Util is what you are looking for.

Solution 10:[10]

You can use Statistics::Lite to compute min,max etc

Solution 11:[11]

Use the List::Util module, which it is recommended to get acquainted with anyway, just like List::MoreUtils:

D:\ :: perl -MList::Util=max -lwe "print max 324, 43, 53, 3532, 43"
3532

D:\ :: perl -MList::Util=min -lwe "print min 324, 43, 53, 3532, 43"
43

Solution 12:[12]

List::Util has the "max" and "min" functions that you can use to directly find the maximum and minimum given a list of numbers. Check if you can use that. You may also sort the array and then determine the highest and lowest number

Solution 13:[13]

This is very late, but this is how I did it without any modules.

sub min {
    my $min = shift;
    foreach (@_) {
        $min = $_ if $_ < $min;
    }
    return $min;
}

sub max {
    my $max = shift;
    foreach (@_) {
        $max = $_ if $_ > $max;
    }
    return $max;
}