
xyz
New User
Feb 22, 2010, 4:31 AM
Post #1 of 4
(449 views)
|
|
Calculation of median value
|
Can't Post
|
|
Hello, I would like to calculate a median value from array. I retrieve data for the array from the following file: However I would like to read three lines and put them to the array. After that I would like to get the median of the three values. If it is not possible to retrieve three lines because they are only two than I would like get the median of the two values. I have tried to do it, but I have got just the output of the file and the median values.
use Data::Dumper; use Carp; my @numArray = (); my $count = 0; open( my $input_fh, '<', "test/median.txt") or croak("Can't open file $!.\n"); while (my $line = <$input_fh> ) { print $line; if ($count < 3) { push(@numArray, $line); $count += 1; } elsif ($count == 2) { my($medianValue) = Math::NumberCruncher::Median(\@numArray); print "Median " . $medianValue; } else { #print Dumper(\%$numArray); } } How could I improve the code. Best regards,
(This post was edited by xyz on Feb 22, 2010, 4:34 AM)
|