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;

Recommended Answers

All 7 Replies

Hi Makailah,

For the input, your program is correct. I assume you forgot to print the result.
Place the below codes at the end of the program. that shows your result of the program.

print "\nColumn 1 Average Value = ", $col1;
print "\nColumn 2 Average Value = ", $col2;
print "\nColumn 3 Average Value = ", $col3;

Wow what a silly mistake on my part. Thank you for your insight.

Hi Makailah,

For the input, your program is correct. I assume you forgot to print the result.
Place the below codes at the end of the program. that shows your result of the program.

print "\nColumn 1 Average Value = ", $col1;
print "\nColumn 2 Average Value = ", $col2;
print "\nColumn 3 Average Value = ", $col3;

I know you helped earlier and I have a similar problem but when I use the above code my averages are incorrect. Is there something wrong with the code?

Try this code,

use 5.010;
my (@arr, %arr, $row);
while(<DATA>){
@arr=split(/\s+/, $_);
for (my $i=1; $i<=$#arr; $i++){
$arr{$i}+=$arr[$i];
}
$row++;
}
say ("total of column $_ is $arr{$_} and average is \t", ($arr{$_}/$row)) for (sort keys (%arr));
__DATA__
1 2 3 4 5
6 6 6 4 4
2 3 4 5 6

try this code Revised

use 5.010;
my (@arr, %arr, $row);
while(<DATA>){
@arr=split(/ +/g, $_);
for (my $i=1; $i<=$#arr; $i++){
$arr{$i}+=$arr[$i];
}
$row++;
}
say ("total of column ",  ($_+1),  " is $arr{$_} and average is \t", ($arr{$_}/$row)) for (sort keys (%arr));



__DATA__
1 2 3 4 5
6 6 6 4 4
2 3 4 5 6
2 3 4 5 6

try this code Revised

use 5.010;
my (@arr, %arr, $row);
while(<DATA>){
@arr=split(/ +/g, $_);
for (my $i=1; $i<=$#arr; $i++){
$arr{$i}+=$arr[$i];
}
$row++;
}
say ("total of column ",  ($_+1),  " is $arr{$_} and average is \t", ($arr{$_}/$row)) for (sort keys (%arr));



__DATA__
1 2 3 4 5
6 6 6 4 4
2 3 4 5 6
2 3 4 5 6

Thank you for your input. I wanted to ask if there was a way for my code to have the file name passed as a command line argument? Thank you

yes u can use @ARGV for getting inputs from Command line arguments. or you can use diamond operator.
cmd:>perl script.pl filename
e.g., perl first.pl matrix.txt;

use 5.010;
my (@arr, %arr, $row);
while(<>){
@arr=split(/ +/g, $_);
for (my $i=1; $i<=$#arr; $i++){
$arr{$i}+=$arr[$i];
}
$row++;
}
say ("total of column ",  ($_+1),  " is $arr{$_} and average is \t", ($arr{$_}/$row)) for (sort keys (%arr));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.