MAX MIN Funktion in Perl

This is a little example, the max_value returns the highest number of a Perl array and min_value the lowest.

#!/usr/bin/perl
use strict;

#example array
my @test = ('2','7','5','9','8','4','6','1','-3');

#test 
print max_value(@test).$/;
print min_value(@test).$/;

#max
sub max_value {
    my @data = @_;
    my $result = $data[0];
    for my $i (0 .. $#data){
        if($data[$i] > $result){
            $result = $data[$i];
        }
    }
    return($result);
}

#min
sub min_value {
    my @data = @_;
    my $result = $data[0];
    for my $i (0 .. $#data){
        if($data[$i] < $result){
            $result = $data[$i];
        }
    }
    return($result);
}

And as result we get the highest and lowest number of our test array.

9
-3

One thought on “MAX MIN Funktion in Perl”

  1. It’s a pity you don’t have a donate button! I’d
    certainly donate to this excellent blog! I suppose for now i’ll settle for bookmarking and adding
    your RSS feed to my Google account. I look forward to brand new
    updates and will talk about this blog with my Facebook
    group. Chat soon!

Leave a Reply

Your email address will not be published.

1 + sixteen =