Let's find the minimum value
I will explain how to find the minimum value using a Perl program.
Let's find the minimum value of the
array.
Consider an array that contains only positive numbers from 1 to 10, such as:
my @array = (8, 1, 3, 5, 7);
Sample program to find the minimum value of an array
Actually find the minimum value from the array. The List::Util module has a min function, which is not used in this example.
You can find the minimum value by preparing a variable to store the minimum value and comparing it with the elements of the array one by one.
* Since the initial value of the variable that saves the minimum value is set to be compared with the numerical value from 1 to 10 in advance, 99, which is sufficiently large, is set as the initial value.
If you don't know the range of numbers to compare, you need to devise a comparison.
use strict;
use warnings;
my @array = (8, 1, 3, 5, 7);
my $min = 99;
for my $num (@array) {
if ($min > $num) {
$min = $num;
}
}
print "$min\n";
Execution result
1
I think that the process is very easy to understand by comparing each one.
* To simplify the code, we will handle an array that contains only positive numbers from 1 to 10.
Let's find the minimum value of a specific column from the tabular data.
Consider the following data that was also used to find the maximum value.
| name | age |
|---|---|
| bob | 9 |
| tom | 14 |
| alice | 11 |
It is as follows when expressed by the reference of the array and the hash.
$array_ref = [
{name =>'bob', age => 9},
{name =>'tom', age => 14},
{name =>'alice', age => 11}
];;
Sample program to find the minimum value of the age column
Data access has become a little more difficult due to the complexity of the referenced data,
The method of checking is the same as before.
use strict;
use warnings;
my $array_ref = [
{name => 'bob', age => 9},
{name => 'tom', age => 14},
{name => 'alice',age => 11}
];
my $min = 99;
for my $info (@{$array_ref}) {
if ($min > $info->{'age'}) {
$min = $info->{'age'};
}
}
print "$min\n";
Execution result
9
The process is the same as before except that the reference is dealt with.
You can copy and paste this program and try it immediately at PerlBanjo.
Perl Data Analytics Tutorial