#!/usr/bin/perl
#usage: ./mm.pl <Мю с крышей(см. задание)> <номер первой строки из таблицы> <номер посл. строки>
open(fil,$ARGV[0]) or die "unable to open file: $!\n";
$sum=0;
$max=0;
$min=1000;
while(<fil>){
	unless(/\A\Z/){
		$list[@list]=$_;
		if($max<$_){
			$max=$_;
		}
		if($min>$_){
			$min=$_;
		}
		$sum+=$_;
		
	}
}
close(fil);
sub numerically { $a<=>$b }
@prir=(3.4,17.6,-7.9,6,-8,31.2,13,28.2,-15.9,23,27.3,6.5,7.4,-7.7,22.2,13.8,-3,-5.6,-8.3,-2,27.9,5.2,-12.4,8.6,42.3,-12.2,-1.8,18,12.4,21,21.4,-3.7,59.1,32,25.6,19.7,-7.1,-15.3,38.8,72.9,4,10,1.7,5.9,-13.5,-5.5,-2.2,8.2,-7.1,-9.7,11.4,94.6,37.5,79.5,41.2,4.8,72.3,24.9,-9.3,-0.7,-18.6,14.8,-6.8,-4.9,41,51.1,39.9,-1.4,-3.1,13.9,2.3,2,-5.7,46.8,48.1,-7.7,0.7);
$avg=$sum/@list;
$nn6_1=0;
$sum6_1=0;
for ($nm=0;$nm<@list;$nm++){
	if($list[$nm]<$ARGV[1]){
		$Nii++ if ($prir[$nm+$ARGV[2]-1]<0);
		$Nit++;
		$nn6_1++;
		$sum6_1+=$list[$nm];
	}
	$sum2+=($list[$nm]-$avg)**2;
}
$nn6_2=@list-$nn6_1;
$sum6_2=$sum-$sum6_1;
$h=($max-$min)/6;
@interv=(0,0,0,0,0,0,0);
@list = sort numerically @list;
foreach $row (@list){
	$interv[int(($row-$min)/$h)]++;
	print $row;
}
$Nti=0;
for ($nn = $ARGV[2]-1; $nn < $ARGV[3]; $nn++){
	$Nti++ if ($prir[$nn]<0);
}
$interv[5]+=$interv[6];
$ind1=int((@list-1) / 2);
$ind2=int(@list / 2);
print "-"x20 . "\n";
print "шаг: $h\n";
print "сумма: $sum\n";
print "кол-во:" . @list . "\n";
print "min: ${min}выборочное среднее: $avg\nmax: $max";
print "размах: " . ($max - $min) . "\n";
print "медиана: " . ((@list[$ind1]+@list[$ind2])/2) . "\n\nТаблица частот(6 интервалов):\n";
for ($num=0;$num<6;$num++){
	printf ("[%.2f; %.2f]: %d\n",($min+$num*$h),($min+($num+1)*$h),$interv[$num]);
}
print "Мю с крышей: " . $ARGV[1] . "\n";

$dispers=$sum2/(@list-1);
$smesh=$dispers**0.5;
print "Эн-один-точка: " . $Nit . "\n";
print "Кол-во городов с отриц. приростом: " . $Nti . "\n";
print "Эн-один-один: " . $Nii . "\n";
print "\nнесмещ. оценка дисперсии: $dispers\n";
print "стандартн. откл-е: $smesh\n\n";
$avg1=$sum6_1/$nn6_1;
$avg2=$sum6_2/$nn6_2;
print "Qnt.1: $nn6_1\n";
print "Sum.1: $sum6_1\n";
print "Avg.1: $avg1\n";
print "Qnt.2: $nn6_2\n";
print "Sum.2: $sum6_2\n";
print "Avg.2: $avg2\n";
$sum26_1=0;
$sum26_2=0;
for ($nm=0;$nm<@list;$nm++){
    if($list[$nm]<$ARGV[1]){
    	$sum26_1+=($list[$nm]-$avg1)**2;
	}else{
    	$sum26_2+=($list[$nm]-$avg2)**2;
	}
}
$dispers1=$sum26_1/($nn6_1-1);
$dispers2=$sum26_2/($nn6_2-1);
print "Dsp.1: $dispers1\n";
print "Dsp.2: $dispers2\n";
