On 8/24/07, Kirk Wythers <[EMAIL PROTECTED]> wrote: > > On Aug 23, 2007, at 11:17 PM, Gunnar Hjalmarsson wrote: > > > Kirk Wythers wrote: > >> I don't see how $totals{$year}{$month}{count} ++; is holding the > >> count. > > > > Read about the auto-increment operator in "perldoc perlop". > > OK. I'll try and be more clear to the degree of my ignorance. First, > I do not understand the use of $totals in both the sum of the scalar > tmax, and the incremented count. snip > I think what I am confused by is the relationship between the scalar > $tmax and the (whatever you call it) $totals{$year}{$month}{tmax} snip
The $tmax variable is the current value the fourth field for each line you read. The name of the totals variable is %totals. It is a hash. 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 Hashes can only contain scalar values, but a reference to a hash (or an array) is a scalar, so Perl mimics multi-dimensional data structures using references. You can create hash references in many ways. One common way is to use an anonymous hash reference: my $hashref = { foo => 10, bar => 20 }; You can get at the values stored in a hash ref with the arrow operator: my $scalar = $hashref->{bar}; #$scalar is now 20 So if I want a multi-dimensional hash I can declare it like this: my %hash = ( foo => $hashref bar => { foo => 30, bar => 40 } ); I could get the first level like this my $ref = $hash{foo}; #$ref is now a reference to $hashref and the second like this my $scalar = $ref->{bar}; #$scalar is now 20 You could also do it more directly like this my $scalar $hash{foo}->{bar}; #$scalar is now 20 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; $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/