我想写一个读物的“Perl”文本。 编号一栏,
20 30 12
31 20 54
63 30 21
11 12 10
and do some calculations, for example the mean. I don t know how to declare and initialize it. I ve got this example, in which is looking for the median, and it has the data declared, in my case, the data is in a file, not in the script and want to calculate the median. there is it.. #!/usr/bin/perl
#data points
@vals = ( 33, 23, 55, 39, 41, 46, 38, 52, 34, 29, 27, 51, 33, 28 );
print "UNSORTED: @vals
";
#sort data points
@vals = sort(@vals);
print "SORTED: @vals
"; #test to see if there are an even number of data points
if( @vals % 2 == 0) {
#if even then:
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum/2;
print "The median value is $med
";
}
else{
#if odd then:
print "The median value is $vals[@vals/2]
";
}
exit;