By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

strange average problem and loop

P: 55
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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my (@z, @pxx,@pyy,@pzz,@gamma) = ();
  5. my $count =0;
  6. my ($gamma,@folder,$folder);
  7. my ($z,$zval,$pxx,$pyy,$pzz,$pxxval,$pyyval,$pzzval);
  8. my ($zsum,$zcount,$pxxsum,$pxxcount,$pyysum,$pyycount,$pzzsum,$pzzcount) = 0;
  9.  
  10. my ($gammaval,$gammasum,$gammacount);
  11. my $path = "/home/rk/surf";
  12. my @temp = ();
  13. opendir(FO,"$path") || die "Please check for $path $!";
  14. @folder = grep(/step_/,readdir(FO));
  15. #print "@folder\n";
  16. closedir(FO);
  17.  
  18. foreach my $ff(@folder)
  19. {
  20.     opendir(FOLDER,"$path/$ff") || die $!;
  21.     my @files = grep {/\.st$/} readdir(FOLDER);
  22.     #print "@files\n";
  23.     close(FOLDER);
  24.  
  25. #    #open(WRITE,">$path/$ff/$name");
  26.     foreach my $file(@files)
  27.     {
  28.         open(PDB,"$path/$ff/$file") or die "Please check for the file";
  29.         my $name = $file;
  30. #       #print "$name\n";
  31.         $name=~ s/\.st/\.avg/g;
  32.         #print "$name\n";
  33. #       #$count +=1;
  34. #       open(WRITE,">$path/$ff/$name");
  35.         while(<PDB>)
  36.         {
  37.             chomp $_;
  38.             my @temp = split(/\t/,$_);
  39.             $z = $temp[0]; $pxx = $temp[1]; $pyy = $temp[2]; $pzz = $temp[3]; $gamma = $temp[4];
  40.             push(@z,$z); push(@pxx,$pxx); push(@pyy,$pyy); push(@pzz,$pzz); push(@gamma,$gamma);
  41.         }
  42.         #@temp = ();
  43.         #print "@z\n";#@pxx,@pyy,@pzz,@gamma\n";
  44.         #close(PDB);
  45.  
  46.             foreach $zval(@z)
  47.             {
  48.                 $zsum += $zval;
  49.                 $zcount +=1;
  50.  
  51.             }my $avgz     = $zsum/$zcount        ;print "$avgz\t";
  52.         @z =();
  53.             foreach $pxxval(@pxx)
  54.             {
  55.                 $pxxsum += $pxxval;
  56.                 $pxxcount +=1;
  57.  
  58.             }my $avgpxx   = $pxxsum/$pxxcount    ;print "$avgpxx\t";
  59.  
  60.         @pxx = ();
  61.             foreach $pyyval(@pyy)
  62.             {
  63.             {
  64.                 $pyysum += $pyyval;
  65.                 $pyycount +=1;
  66.             }my $avgpyy   = $pyysum/$pyycount    ;print "$avgpyy\t";
  67.         @pyy = ();
  68.             foreach $pzzval(@pzz)
  69.             {
  70.                 $pzzsum += $pzzval;
  71.                 $pzzcount +=1;
  72.             }my $avgpzz   = $pzzsum/$pzzcount    ;print "$avgpzz\t";
  73.         @pzz = ();
  74.             foreach $gammaval(@gamma)
  75.             {
  76.                 $gammasum += $gammaval;
  77.                 $gammacount +=1;
  78.             }my $avggamma = $gammasum/$gammacount;print "$avggamma\n";
  79.         @gamma = ();
  80.     }close (PDB);@temp = ();
  81. }
  82.  
thanks for the help
kumar
Apr 18 '08 #1
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
Are you getting any errors? What is happens on the rest of the files that does not happen on the first one? I would consider a hash to hold the values then do your averaging.

--Kevin
Apr 19 '08 #2

nithinpes
Expert 100+
P: 410
The variables $zsum, $pxxsum,$pyysum, $pzzsum, $gammasum, $zcount, $pxxcount, $pyycount, $pzzcount, $gammacount should be re-initialized for each file (with each iteration of 'foreach my $file(@files)' ). Use the 'my' scope modifier to declare these variables at the begining of the above said foreach() block.

Expand|Select|Wrap|Line Numbers
  1. foreach my $file(@files)
  2.     {
  3.        my ($zsum, $zcount, $pxxsum, $pxxcount, $pyysum, $pyycount, $pzzsum, $pzzcount);
  4.        my ($gamma,@folder,$folder);
  5.         open(PDB,"$path/$ff/$file") or die "Please check for the file";
  6.         my $name = $file;
  7. #
  8. #
  9. #
  10.  
In your script, you have declared the above variables at the begining of script. Because of this the value that these variables get in a previous iteration of foreach will be retained for the current iteration and as a result the sum and count will be cumulative of all files processed.

Also, make a note to include comments while posting your code. This would help someone looking into the code to directly get to the problematic block.
Apr 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.