strange average problem and loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    strange average problem and loop

    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.
    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 = ();
    }
    thanks for the help
    kumar
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    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

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      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.

      Code:
      foreach my $file(@files)
          {
             my ($zsum, $zcount, $pxxsum, $pxxcount, $pyysum, $pyycount, $pzzsum, $pzzcount);
             my ($gamma,@folder,$folder);
              open(PDB,"$path/$ff/$file") or die "Please check for the file";
              my $name = $file;
      #
      #
      #
      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.

      Comment

      Working...