Hi all,

I try to find the average of colum with perl.
I find in forum that have the question http://www.daniweb.com/software-development/perl/threads/328723.

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

But I have problems with script. If the DATA have not a number it is erorr.
For example:
__data__
#########
1 2 3 4 5

6 6 6 4 4
2 3 4 5 6
Could you please show me what the problem and how to solve that.
Thank you very much.

Recommended Answers

All 4 Replies

You need to name your DATA section __DATA__ in capital letters, not __data__ .

You can skip records that don't begin with a number, as follows:

#skip non-numeric data
next unless m/^\d/;

Notice that the script ignores column 0 and starts with column 1, because in Perl array indexes begin at 0. Is that what you want?

thank you very much.
It is run well.
I had repair

for (my $i=1; $i<=$#arr; $i++){$arr{$i}+=$arr[$i];}{

but it was printed begin with the "Colum 0".

total of colum 0 is 9 and average is 3 
      total of colum 1 is 11 and average is 3.6666666 
     .................................................

How can I set up the colum 0 to become colum 1? It means "total of colum 1 is 9 and average is 3".
By the way, could you please show me where I could study the mean of symbol in perl and how to use it?(EX: what mean of m/^\d/)

thank you very much.
It is run well.
I had repair

for (my $i=1; $i<=$#arr; $i++){$arr{$i}+=$arr[$i];}{

but it was printed begin with the "Colum 0".

total of colum 0 is 9 and average is 3 
      total of colum 1 is 11 and average is 3.6666666 
     .................................................

How can I set up the colum 0 to become colum 1? It means "total of colum 1 is 9 and average is 3".
By the way, could you please show me where I could study the mean of symbol in perl and how to use it?(EX: what mean of m/^\d/)

I think you would need to convert your one-line for loop statement into a multi-statement for loop so you can create a variable for column number that will be $_ plus one.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

my (@arr, %arr, $row);
while(<DATA>){
    #skip non-numeric data
    next unless m/^\d/;
    
    @arr=split(/\s+/, $_);
    for (my $i=0; $i<=$#arr; $i++){
        $arr{$i}+=$arr[$i];
    }
    $row++;
}

#When $_ = 0 you want to call it column 1, etc.
for (sort keys (%arr)){
    my $col_nbr = $_ + 1;
    say ("total of column $col_nbr is $arr{$_} and average is \t", ($arr{$_}/$row));
}

__DATA__
#########
1 2 3 4 5

6 6 6 4 4
2 3 4 5 6

To find the meaning of m/^\d/ you can Google Perl regular expressions. The ^ means the beginning of the record and \d represents any numeric digit so m/^\d/ will match any data record that begins with a number. Look for \d and other symbols in the Perl regular expression tutorial.

thank you very much for tutorial.

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.