On 8/24/07, Kirk Wythers <[EMAIL PROTECTED]> wrote:
snip
> This is part of what was confusing me. I didn't see %totals declared
> anywhere is the suggestion.
snip

Yeah, you can only tell it is a hash because of how it is used.  I
believe the person who posted the example expected you to know what a
hash was and how to declare it.

snip
> Then, should I be declaring my%totals right up top and treating the
> variables like this?
>
> #! /usr/bin/perl -w
>
> use strict;
> use warnings;
>
> $, = ' '; # set output field separator
> $\ = "\n"; # set output record separator
>
> my%totals;
>
>   my ( $year, $month, $doy, $tmax, $tmin, $par, $precip, $NH4, $NO3,
> $O3, $CO2, $V1, $V2, $V3, $V4 ) = split;
> # store totals by month as:
>
>   $totals{$year}{$month}{doy} += $doy;
>   $totals{$year}{$month}{tmax} += $tmax;
>   $totals{$year}{$month}{tmin} += $tmin;
>   $totals{$year}{$month}{par} += $par;
>   $totals{$year}{$month}{precip} += $precip;
>   etc....
>
> # store the count
>   $totals{$year}{$month}{count} ++;
>
> print $year,  $totals{$year}{$month}{doy} /  $totals{$year}{$month}
> {count}, $totals{$year}{$month}{tmax} / $totals{$year}{$month}
> {count}, $totals{$year}{$month}{tmin} / $totals{$year}{$month}{count,
> etc....
snip

Yes, assuming that the += code is inside a loop that reads lines from
a file.  Also, I am incredibly lazy and the thought of typing all of
that code gives me the shivers, so I would say

#!/usr/bin/perl

use strict;
use warnings;

my @sum = qw<tmax tmin par precip NH4 NO3 O3 CO2 V1 V2 V3 V4>;
my %totals;

while (<>) {
    chomp;
    my %rec;
    my ($y, $m, $doy);
    #put year, month, and doy in $y, $m, and $doy and the rest in %rec
    ($y, $m, $doy, @[EMAIL PROTECTED]) = split;
    $totals{$y}{$m}{$_} += $rec{$_} for @sum;
    $totals{$y}{$m}{count}++;
}

for my $y (sort keys %totals) {
    for my $m (sort keys %{$totals{$y}}) {
        my $rec =  $totals{$y}{$m};
        my @avg =  map { $rec->{$_}/$rec->{count} } @sum;
        print join("\t", $y, $m, @avg), "\n";
    }
}


If I decided later that some columns should be summed and others
averaged then I could change it to

#!/usr/bin/perl

use strict;
use warnings;

#fields to sum
my @sum = qw<tmax tmin par precip NH4 NO3 O3 CO2 V1 V2 V3 V4>;
#fields to average
my @avg = qw<tmax tmin par NH4 NO3 O3 CO2 V1 V2 V3 V4>;
my %totals;

while (<>) {
    chomp;
    my %rec;
    my ($y, $m, $doy);
    #put year, month, and doy in $y, $m, and $doy and the rest in %rec
    ($y, $m, $doy, @[EMAIL PROTECTED]) = split;
    $totals{$y}{$m}{$_} += $rec{$_} for @sum;
    $totals{$y}{$m}{count}++;
}

for my $y (sort keys %totals) {
    for my $m (sort keys %{$totals{$y}}) {
        my %rec;
        #make a copy of the data so totals can be used for other things
        @[EMAIL PROTECTED], 'count'} = @[EMAIL PROTECTED],'count'};
        #average only the fields in @avg
        @[EMAIL PROTECTED] =  map { $rec{$_}/$rec{count} } @avg;
        print join("\t", $y, $m, @[EMAIL PROTECTED]), "\n";
    }
}

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


Reply via email to