On Mon, Jan 21, 2002 at 10:50:06AM -0500, Bernie Cosell wrote: > On 21 Jan 2002, at 15:12, Simon Cozens wrote: > > > On Mon, Jan 21, 2002 at 03:00:57PM +0000, Robin Houston wrote: > > > In an ideal world it would behave the same as > > > %a = (%b, %c); > > > for my $k (keys %a) { > > > $a{$k} += $b{$k} if exists($b{$k}) && exists($c{$k}); > > > } > > > > It's not impossible that it would end up doing just that. > > Is that right? --- that is, add b to a on condition of c, but the actual > value from the c hash isn't used at all, and a is *incremented* even > though it looks like an assignement [you removed it from your followup, > but I think the original was: > %a = %b ^+ %c > That's *REALLY unintuitive [at least to me] to have it work with the > above semantics.
I think you're missing the first line, which says: %a = (%b, %c); which initializes %a to have *all* keys and values from %c. In addition to what's in %c, %a will also have the key/value pairs from %b that are not in %c. It's in this first line that %c is being used. For all the keys $k in %a (which includes all the keys in %c - the for() line could have been written as 'for my $k (keys %c)' as well), if $k exists in both %b and %c (which would mean $a {$k} equals $c {$k}), we're adding the value of $b {$k}. This results in $a {$k} == $b {$k} + $c {$k}. For all keys that are in either %b or %c, but not both, %a will get the key with the corresponding value. Abigail