Hi all,
I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first 3 values and then 4 values and then last 3 value and then calculate the three averages. I have written a code but I guess it is very good way to calculate it and sometimes i get garbage values also.
Here is a data file:
[CODE=perl]
1
2
3
4
5
6
7
8
9
10
[/CODE]
so when the user specifies the different parts as arguments, the average should be calculated.
Here is my perl code:
[CODE=perl]
#!/usrbin/perl
use strict;
use warnings;
my $file = $ARGV[0];
my $cut1 = $ARGV[1];
my $cut2 = $ARGV[2];
my $cut3 = $ARGV[3];
my (@tt,$result1,$ result2,$result 3);
open (A,$file);
my ($count,$total) = 0;
my ($val,$result);
while (<A>)
{
my @temp = split (/\s+/,$_);
$val = $temp[1];
$total = $total + $val;
$count++;
if ($count == $cut1)
{
$result1 = sprintf("%.3f", $total/$count);
push(@tt,$resul t1);
my $s1= $total;
my $r1 = $total/$count;
print "$s1\t$count\t$ r1\n";
}
if ($count == ($cut2+ $cut1))
{
$result2 = sprintf("%.3f", ($total-($result1*$cut1 ))/($count-$cut1));
push(@tt,$resul t2);
my $s2=($total -($result1*$cut1 ));
my $c2 = ($count - $cut1);
my $r2 = $s2/$c2;
print "$s2\t$c2\t$r2\ n";
}
if ($count == ($cut3+$cut2+$c ut1))
{
$result3 = $total- (($result1*$cut 1) + ($result2*$cut2 )) / ($count- ($cut1+$cut2));
push(@tt,$resul t3);
my $s3 = ($total- (($result1*$cut 1) + ($result2*$cut2 )));
my $c3 = $count - ($cut1+$cut2);
my $r3 = $s3/$c3;
print "$s3\t$c3\t$r3\ n";
}
}
my $add = 0;
foreach my $r(@tt)
{
$add = $add +$r;
}
print "$add/scalar(@tt)\t";
my $final = sprintf("%.3f", $add/scalar(@tt));
print "$final\n";
[/CODE]
Right now i can take 3 user cutoffs but if i want to make this program take any number of cutoffs to calculate the averages.
I guess there must a better way to calculate the average from different section of same data file.
Here I have used just 1 to 10 numbers as examples but my actual data files have 16900 lines and i have to calculate the average by using different parts of the file.
Any help in this regard is appreciated.
Thanks
Kumar
I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first 3 values and then 4 values and then last 3 value and then calculate the three averages. I have written a code but I guess it is very good way to calculate it and sometimes i get garbage values also.
Here is a data file:
[CODE=perl]
1
2
3
4
5
6
7
8
9
10
[/CODE]
so when the user specifies the different parts as arguments, the average should be calculated.
Here is my perl code:
[CODE=perl]
#!/usrbin/perl
use strict;
use warnings;
my $file = $ARGV[0];
my $cut1 = $ARGV[1];
my $cut2 = $ARGV[2];
my $cut3 = $ARGV[3];
my (@tt,$result1,$ result2,$result 3);
open (A,$file);
my ($count,$total) = 0;
my ($val,$result);
while (<A>)
{
my @temp = split (/\s+/,$_);
$val = $temp[1];
$total = $total + $val;
$count++;
if ($count == $cut1)
{
$result1 = sprintf("%.3f", $total/$count);
push(@tt,$resul t1);
my $s1= $total;
my $r1 = $total/$count;
print "$s1\t$count\t$ r1\n";
}
if ($count == ($cut2+ $cut1))
{
$result2 = sprintf("%.3f", ($total-($result1*$cut1 ))/($count-$cut1));
push(@tt,$resul t2);
my $s2=($total -($result1*$cut1 ));
my $c2 = ($count - $cut1);
my $r2 = $s2/$c2;
print "$s2\t$c2\t$r2\ n";
}
if ($count == ($cut3+$cut2+$c ut1))
{
$result3 = $total- (($result1*$cut 1) + ($result2*$cut2 )) / ($count- ($cut1+$cut2));
push(@tt,$resul t3);
my $s3 = ($total- (($result1*$cut 1) + ($result2*$cut2 )));
my $c3 = $count - ($cut1+$cut2);
my $r3 = $s3/$c3;
print "$s3\t$c3\t$r3\ n";
}
}
my $add = 0;
foreach my $r(@tt)
{
$add = $add +$r;
}
print "$add/scalar(@tt)\t";
my $final = sprintf("%.3f", $add/scalar(@tt));
print "$final\n";
[/CODE]
Right now i can take 3 user cutoffs but if i want to make this program take any number of cutoffs to calculate the averages.
I guess there must a better way to calculate the average from different section of same data file.
Here I have used just 1 to 10 numbers as examples but my actual data files have 16900 lines and i have to calculate the average by using different parts of the file.
Any help in this regard is appreciated.
Thanks
Kumar
Comment