Thank you very much for the explanation Chas. It is starting to make
more sense. The reason I was attracted to the solution
#! /usr/bin/perl -w
use strict;
use warnings;
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator
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}{tmax} += $tmax;
...
# store the count
$totals{$year}{$month}{count} ++;
# After you read all the input, you can calculate the averages.
Is that is seems to allow for easy re-arranging of output. For
example I can envision the need to change precip to a monthly total
(the sum), instead of the monthly average.
On Aug 24, 2007, at 12:52 PM, Chas Owens wrote:
The name of the totals variable is %totals. It is a hash.
This is part of what was confusing me. I didn't see %totals declared
anywhere is the suggestion.
A hash is
like an array, but instead of using numbers as offsets it uses strings
as keys (it is also unordered). Elements in the hash are accessed by
giving the hash a key like this:
my %hash = (
foo => 10;
bar => 20;
);
my $scalar = $hash{foo}; #scalar is now 10
Now, this happens often enough and is fairly unambiguous, so Perl has
some syntactic sugar that lets drop the arrow operator between {} and
[] operators. So you could write the last example more clearly as
my $scalar $hash{foo}{bar}; #$scalar is now 20
In addition, Perl also has something called auto-vivification that
causes hash references to auto-magically come into existence when
needed, so I can say
my %hash;
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....
Kirk
$hash{foo}{bar} = 10;
and the hash reference needed to store the second level will
auto-magically come into existence. This is how
$total{$year}{$month}{tmax} += $tmax;
works. It is creating a three level deep hash and storing the totals
in the last level.
snip
Therefore, if I want the sum of tmax, I'm not sure what is holding
the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}?
The same goes for count. What is the holding the count?
snip
After the loop finishes the sum of all of the tmax fields for a given
year and month will be in
$total{$year}{$month}{tmax}
where $year is the year you want and $month is the month you want.
I guess I'm just used to the very simple $ as defining the scalar.
$ does is the sigil associated with scalar values, which is why, in
Perl 5, you also see them when referring single element (which is a
scalar) in an array or hash (you see @ when referring to multiple
values, as my solution did frequently).
snip
SImilarly, if I want the average of $tmax, then I would want to
divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count}
snip
Yes.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/