sridhar56 has asked for the wisdom of the Perl Monks concerning the following question:

I have a directory of text files and I want to process each file in that directory using the subroutine average to calculate the average for a file. However, my script processes the same file every time. I am not sure what is the mistake I am doing here. Below is the code I wrote:
#!/usr/bin/perl #Naga_betrapally_6_3_16 use strict; use warnings; use Cwd; use FileHandle; my $name; + # Naming of variables, remembers the name of the +file my $cwd = getcwd(); + #Takes in the current directory of files my $opendir = "$cwd"; + #Used to open the current directory and remembers t +he location opendir (DIR, $opendir) or die $!; + #Open directory command my @directory = readdir DIR; + #Grabbing all the fas files stored in the location my $scalar = scalar(@directory); + #Counting the number of files and printing it in th +e next line #print "The number of sample files in this folder: $scalar \n"; # for (my $i=0; $i<scalar(@directory); $i++){ # $name = "$directory[$i]"; # print "The file read in: $name\n"; # my $digits = substr $name, 0, 4; + #Grabbing the first 4 characters to name the sample + run for MEGA # my $tmp = "$opendir/$directory[$i]"; # } for(my$i=0;$i<@directory;$i++){ print $directory[$i]; &average($directory[$i]); } #average(@directory); sub average { my $header; my @array_lines; my $n=0; my $filename=@_; + #Takes in the sequence file from the user inpu +t with the year in the header my @average_array; my $sum =0; my $avarage; next if($filename =~ /^\.$/); next if($filename =~ /^\.\.$/); open (FILE,$filename) or die "No input file provided or the input file + does not exist in the path, '$_' $!"; print "$filename\n"; while (my $file = <FILE>){ chomp $file; if ($file =~/^Name/){ $header = $file; } else{ $array_lines[$n] = $file; chop $array_lines[$n]; $n++; } } for(my $i=0;$i<@array_lines;$i++){ my @tmp = split(/\t/,($array_lines[$i])); #print $tmp[3]; push @average_array, $tmp[3]; my $scalar = scalar(@average_array); #print $scalar; } foreach (@average_array){ #print "$_\n"; my $tmp = $_; $tmp =~ s/,//; #print "$tmp\n"; $sum = $sum+$tmp; } #print "The sum of all the mapping is $sum\n"; $avarage = $sum/11; #print "THe average of the mapping is $avarage\n"; close (FILE); }
All the files in that folder maintain the same structure as the sample file below:
Name Consensus length Total read count Average coverage Re +ference sequence Reference length NSP4-E1 mapping 750 227760 64,516.84 NSP4-E1 750 VP7-G3 mapping 1062 303758 63,062.33 VP7-G3 1062 Rotarix-VP6 mapping 1356 161491 26,020.50 Rotarix-VP6 1 +356 Rotarix-NSP1 mapping 1559 114394 16,077.21 Rotarix-NSP1 + 1568 NSP2-N1 mapping 1059 80424 16,178.61 NSP2-N1 1058 NSP5-H1 mapping 664 75269 23,943.71 NSP5-H1 663 VP1-R1 mapping 3302 69677 4,542.15 VP1-R1 3302 VP2-C1 mapping 2717 149312 11,987.00 VP2-C1 2717 VP3-M1 mapping 2591 31795 2,632.39 VP3-M1 2591 RotaTeq-WI79-4-VP4 mapping 2359 78305 7,223.08 RotaTeq-WI7 +9-4-VP4 2359 NSP3-T1 mapping 1074 173210 34,910.87 NSP3-T1 1074

Replies are listed 'Best First'.
Re: Subroutine processes only one file in the directory
by toolic (Bishop) on May 25, 2017 at 19:31 UTC
    I see one mistake. Change:
    my $filename = @_;

    to:

    my $filename = $_[0];

    Your code always assigns $filename=1, instead of the actual file name. Prove this to yourself by printing $filename inside your sub right after the assignment.

    See also: Basic debugging checklist

      I did that and it works! And you are right, when I was printing $filename, it returned one! Unfortunately I had a file named 1 and I thought it was only taking the last file in the array! Thanks for the help! :)