hi all,
i am processing a certain number of files from a directory and each file has around 5 columns and 200 rows with tab separated values.
I am trying to grab each file and calculate the average value for each column and print it. Code works fine for the first file but the calculations for the 2nd file and onwards is wrong. May be some can provide me help.
thanks for the help
kumar
i am processing a certain number of files from a directory and each file has around 5 columns and 200 rows with tab separated values.
I am trying to grab each file and calculate the average value for each column and print it. Code works fine for the first file but the calculations for the 2nd file and onwards is wrong. May be some can provide me help.
Code:
#!/usr/bin/perl use strict; use warnings; my (@z, @pxx,@pyy,@pzz,@gamma) = (); my $count =0; my ($gamma,@folder,$folder); my ($z,$zval,$pxx,$pyy,$pzz,$pxxval,$pyyval,$pzzval); my ($zsum,$zcount,$pxxsum,$pxxcount,$pyysum,$pyycount,$pzzsum,$pzzcount) = 0; my ($gammaval,$gammasum,$gammacount); my $path = "/home/rk/surf"; my @temp = (); opendir(FO,"$path") || die "Please check for $path $!"; @folder = grep(/step_/,readdir(FO)); #print "@folder\n"; closedir(FO); foreach my $ff(@folder) { opendir(FOLDER,"$path/$ff") || die $!; my @files = grep {/\.st$/} readdir(FOLDER); #print "@files\n"; close(FOLDER); # #open(WRITE,">$path/$ff/$name"); foreach my $file(@files) { open(PDB,"$path/$ff/$file") or die "Please check for the file"; my $name = $file; # #print "$name\n"; $name=~ s/\.st/\.avg/g; #print "$name\n"; # #$count +=1; # open(WRITE,">$path/$ff/$name"); while(<PDB>) { chomp $_; my @temp = split(/\t/,$_); $z = $temp[0]; $pxx = $temp[1]; $pyy = $temp[2]; $pzz = $temp[3]; $gamma = $temp[4]; push(@z,$z); push(@pxx,$pxx); push(@pyy,$pyy); push(@pzz,$pzz); push(@gamma,$gamma); } #@temp = (); #print "@z\n";#@pxx,@pyy,@pzz,@gamma\n"; #close(PDB); foreach $zval(@z) { $zsum += $zval; $zcount +=1; }my $avgz = $zsum/$zcount ;print "$avgz\t"; @z =(); foreach $pxxval(@pxx) { $pxxsum += $pxxval; $pxxcount +=1; }my $avgpxx = $pxxsum/$pxxcount ;print "$avgpxx\t"; @pxx = (); foreach $pyyval(@pyy) { { $pyysum += $pyyval; $pyycount +=1; }my $avgpyy = $pyysum/$pyycount ;print "$avgpyy\t"; @pyy = (); foreach $pzzval(@pzz) { $pzzsum += $pzzval; $pzzcount +=1; }my $avgpzz = $pzzsum/$pzzcount ;print "$avgpzz\t"; @pzz = (); foreach $gammaval(@gamma) { $gammasum += $gammaval; $gammacount +=1; }my $avggamma = $gammasum/$gammacount;print "$avggamma\n"; @gamma = (); }close (PDB);@temp = (); }
kumar
Comment