The Other1 wrote:
> Hi all,

Hello,

> Sorta newbie here...
> 
> I have parsed log files and have an array of arrays that looks like this:
> 
> [...]
> 1 0.0 0.000 31.954 36.169 12.645 20:40
> 16 1048.0 16.196 15.825 52.502 5.150 20:40
> 8 1281.7 12.059 9.634 41.264 4.869 20:40
> 9 1157.7 19.094 16.889 52.218 4.742 20:40
> 0 0.0 0.000 76.430 109.366 7.179 20:50
> 0 203.3 7.029 35.411 65.495 10.142 20:50
> 0 167.5 8.476 51.829 78.884 24.049 20:50
> 0 135.3 4.371 33.088 54.554 16.763 20:50
> 2 0.0 0.000 22.612 23.297 13.714 21:00
> 5 260.0 5.233 20.610 46.264 9.826 21:00
> 0 242.3 7.003 29.601 56.070 8.485 21:00
> 0 227.3 5.954 26.831 50.991 10.794 21:00
> 0 0.0 0.000 28.523 56.333 10.554 21:10
> 0 146.9 4.573 31.879 61.403 10.197 21:10
> 0 151.4 4.330 29.295 54.513 11.121 21:10
> 0 134.6 3.911 29.753 57.781 10.771 21:10
> [...]
> 
> 7 values in each row.  4 rows represent 1 data set.  I need to get an
> avearge for each of the values in the first 6 columns and 4 rows each. 
> The last value is a timestamp so I don't need an average on that.  So
> for every 4 rows, I need to end up with 1 row of 7 numbers, the first 6
> being averages and the 7th being the time.
> 
> I have a subfunction, Average, defined like this:
> sub Average {
>    my ($Sum,$Average) = 0;
>    foreach my $item (@_) {
>      $Sum += $item;
>     }
>      my $Average = ($Sum / 4);
>      return $Average;
> }
> 
> My question is how to walk through the array grabbing column 1 of four
> rows, pass those values to the Average function (or if there is a beter
> way, I am open to it!), store the average to be used by GD later, then
> do the next column, etc, up to column 6, then move on to next 4 rows. 
> Any help is much appreciated!  Thanx!

This appears to do what you want:

use warnings;
use strict;

my @data = map [ split ], <DATA>;

my $data_set = 4;

sub get_average {
    my @average;

    for my $row ( @_ ) {
        for my $column ( 0 .. $#$row ) {
            if ( $row->[ $column ] =~ /^[\d.+-]+$/ ) {
                $average[ $column ] += $row->[ $column ];
                }
            else {
                $average[ $column ] = $row->[ $column ];
                }
            }
        }

    for my $column ( @average ) {
        if ( $column =~ /^[\d.+-]+$/ ) {
            $column /= $data_set;
            }
        }

    return @average;
    }

for ( my $index = 0; $index + $data_set <= @data; $index += $data_set ) {

    for ( @data[ $index .. $index + $data_set - 1 ] ) {
        print "@$_\n";
        }

    my @average = get_average @data[ $index .. $index + $data_set - 1 ];
    print '-' x 50, "[EMAIL PROTECTED]", '-' x 50, "\n";
    }


__DATA__
1 0.0 0.000 31.954 36.169 12.645 20:40
16 1048.0 16.196 15.825 52.502 5.150 20:40
8 1281.7 12.059 9.634 41.264 4.869 20:40
9 1157.7 19.094 16.889 52.218 4.742 20:40
0 0.0 0.000 76.430 109.366 7.179 20:50
0 203.3 7.029 35.411 65.495 10.142 20:50
0 167.5 8.476 51.829 78.884 24.049 20:50
0 135.3 4.371 33.088 54.554 16.763 20:50
2 0.0 0.000 22.612 23.297 13.714 21:00
5 260.0 5.233 20.610 46.264 9.826 21:00
0 242.3 7.003 29.601 56.070 8.485 21:00
0 227.3 5.954 26.831 50.991 10.794 21:00
0 0.0 0.000 28.523 56.333 10.554 21:10
0 146.9 4.573 31.879 61.403 10.197 21:10
0 151.4 4.330 29.295 54.513 11.121 21:10
0 134.6 3.911 29.753 57.781 10.771 21:10




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to