Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Basic arithmetic functions

by dimmesdale (Friar)
on Aug 08, 2002 at 17:22 UTC ( [id://188653] : note . print w/replies, xml ) Need Help??


in reply to Basic arithmetic functions

Check out split, it can be real helpful at times.

# real important! use strict; use warnings; # this is the file where the data is my $file = 'path_to_file'; # these are some useful variables my $sum = 0; my $lines = 0; my $avgerage; # open file open(FH, $file) or die "File $file failed to open:"; #read file in line by line while(<FH>) { # get the third column and add it to $sum $sum += (split /\s+/, $_)[2]; # update $lines $lines++; } # now, to get the average, all you do is say $average = $sum/$lines;