Good day, I am new to the site and would like some help on a code. I must write a perl program that gets the average of each column of numbers in any file?
The format is:
1 2 3 4 5
6 6 6 4 4
2 3 4 5 6
My code is below and will not work. Can someone tell me what I am doing wrong? Thank you
$col1 = 0;
$col2 = 0;
$col3 = 0;
$col4 = 0;
$col5 = 0;
$rows = 0;
open (myfile, "source.txt") || die "couldn't open the file!";
while ($record = <myfile>) {
$record =~ m/^(\d)\s(\d)\s(\d)\s(\d)\s(\d)/;
$col1 += $1;
$col2 += $2;
$col3 += $3;
$col4 += $4;
$col5 += $5;
$rows=$rows+1;
}
close(myfile);
$col1 /= $rows;
$col2 /= $rows;
$col3 /= $rows;