in reply to Calculating statistical median

#!/usr/bin/perl -w use strict; my @array = qw/1 2 3 4 9/; print median(@array); sub median { my @vals = sort {$a <=> $b} @_; my $len = @vals; if($len%2) #odd? { return $vals[int($len/2)]; } else #even { return ($vals[int($len/2)-1] + $vals[int($len/2)])/2; } }

Replies are listed 'Best First'.
Re^2: Calculating statistical median
by Anonymous Monk on Jun 16, 2017 at 09:58 UTC
    This is the only sample here that returns correct median. Thanx guy.