Calculate average from CSV file using PERL script

Ok, open your favorite text editor, type in this program and save the file as "fps.pl" in the same directory as "fps.csv":

#!perl -w
my $total   = 0;
my $lines   = 0;
my $infile  = "fps.csv";
my $outfile = "out.txt";

# read $infile and process it
open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
while (<IN>) {
  next if /^\s*$/;                  # ignore blank lines or lines that have whitespaces only
  chomp;                            # remove EOL character
  $total += (split /[,:]/)[1];      # running total of the FPS value
  $lines++;                         # increment the line number
}
close (IN) or die "Can't close $infile: $!";

# now write to $outfile
open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
print OUT $total/$lines;            # average is the total of FPS values divided by linecount
close (OUT) or die "Can't close $outfile: $!";

The inline comments should be self-explanatory.
Once you are at the DOS prompt after saving the Perl program "fps.pl", the "type" command should show something like this:

C:\>
C:\>
C:\>type fps.pl
#!perl -w
my $total   = 0;
my $lines   = 0;
my $infile  = "fps.csv";
my $outfile = "out.txt";

# read $infile and process it
open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
while (<IN>) {
  next if /^\s*$/;                  # ignore blank lines or lines that have whitespaces only
  chomp;                            # remove EOL character
  $total += (split /[,:]/)[1];      # running total of the FPS value
  $lines++;                         # increment the line number
}
close (IN) or die "Can't close $infile: $!";

# now write to $outfile
open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
print OUT $total/$lines;            # average is the total of FPS values divided by linecount
close (OUT) or die "Can't close $outfile: $!";

C:\>
C:\>

Now feed the Perl program to your perl interpreter like so:

C:\>
C:\>perl fps.pl

C:\>
C:\>

Note that no output is displayed in the DOS window. The control simply returns to the next prompt.
However, the output file called "out.txt" should've been created in the same directory as "fps.csv" and "fps.pl".
To display the contents of "out.txt", use the DOS "type" command:

C:\>
C:\>type out.txt
30.1827114285714
C:\>
C:\>

tyler_durden

Hi Durden Tyler

Yeah I got the answer in out.txt. Let say I have fps1.csv, fps2.csv, fps3.csv .... then how to produce so that each log file will have its own result? Basicly my idea is the program will detect each csv file in the current directory, then it will include in array..so the program will load csv file from array..having some problem here..

What problem?

tyler_durden

Hi

I try to modify so it can process many log file and produce result.txt for each file. Seems like my code is not correct

# Find filenames containing 'log' in current working directory
system("dir /b | find \".log\" /I > FPSList.txt");

open(LISTFILE, "<FPSList.txt") || die "Can't find FPSList.txt in current folder. Stopped execution";
@marklist = <LISTFILE>;

open(FILE, "ResultList.txt")
@resultlist=<amifile>;

#!perl -w
my $total   = 0;
my $lines   = 0;
my $infile  = @marklist;
my $outfile = @resullist;

# read $infile and process it
open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
while (<IN>) {
  next if /^\s*$/;                  # ignore blank lines or lines that have only whitespaces
  chomp;                            # remove EOL character
  $total += (split /[,:]/)[1];      # running total of the FPS value
  $lines++;                         # increment the line number

close (IN) or die "Can't close $infile: $!";

# now write to $outfile
open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
print OUT $total/$lines;            # average is the total of FPS values divided by linecount
close (OUT) or die "Can't close $outfile: $!";
}

What makes you think so?
What's the error message?

tyler_durden