Let's find the median

I will explain how to find the median using a Perl program.

Let's find the median of the array

Consider the following array.

my @array1 = (1, 4, 5, 7, 8);
my @array2 = (1, 4, 6, 8, 9, 12);

Sample program to find the median of an array

Actually find the median from the array. The Statistics::Lite module has a median function, but we won't use it in this example.

If the number of elements is odd, the median value is the value in the center of the element, and if the number of elements is even, the average of the two values ​​in the center can be calculated as the median value.

* Sorted data is used to simplify the code.

use strict;
use warnings;

my @array1 = (1, 4, 5, 7, 8); 
my @array2 = (1, 4, 6, 8, 9, 12); 
my $median = 0;

sub get_median {
    my ($array_ref) = @_;
    my $array_cnt = @$array_ref;
    my $ret_median = 0;

    my $median_pos = int($array_cnt / 2);
    if ( $array_cnt % 2 == 1) {
        $ret_median = @$array_ref[$median_pos];
    } else {
        my $median_pos2 = $median_pos - 1;
        $ret_median = (@$array_ref[$median_pos] + @$array_ref[$median_pos2]) / 2;
    }
	return $ret_median;
}

$median = get_median(\@array1);
print "The median of array1 is" . "$median" . "\n";

$median = get_median(\@array2);
print "The median of array2 is" . "$median" . "\n";

Execution result

The median of array1 is 5
The median of array2 is 7

You can get the central element of an array of elements without having to compare values ​​like maximum, minimum, and so on.

Let's find the median value of a specific column from the tabular data.

Consider the following data.

name age
bob 9
alice 11
tom 13

It is as follows when expressed by the reference of the array and the hash.

$array_ref1 = [
  {name =>'bob', age => 9},
  {name =>'alice', age => 11},
  {name =>'tom', age => 13}
];;

Sample program to find the median 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.

* $Array_ref2 is also added to include even patterns.

use strict;
use warnings;
use Data::Dumper;

my $array_ref1 = [
  {name => 'bob', age => 9},
  {name => 'alice',age => 11},
  {name => 'tom', age => 13}
];

my $array_ref2 = [
  {name => 'bob', age => 9},
  {name => 'alice',age => 11},
  {name => 'tom', age => 13},
  {name => 'carol', age => 15}
];

my $median = 0;

sub get_median {
    my ($array_ref) = @_;
    my $array_cnt = @$array_ref;
    my $median_pos = int($array_cnt / 2);
    my $ret_median = 0;

    if ( $array_cnt % 2 == 1) {
	    $ret_median = $array_ref->[$median_pos]->{'age'};
    } else {
        my $median_pos2 = $median_pos - 1;
        my $value1 = $array_ref->[$median_pos]->{'age'};
        my $value2 = $array_ref->[$median_pos2]->{'age'};
       
        $ret_median = ($value1 + $value2) / 2;
    }
    return $ret_median;
}

$median = get_median(\@$array_ref1);
print "The median of array_ref1 is ". "$median". "\ n";

$median = get_median(\@$array_ref2);
print "The median of array_ref2 is:". "$median". "\ n";

Execution result

The median of array_ref1 is 11
The median of array_ref2 is 12

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.

Associated Information