#!/opt/perl/bin/perl -w use strict; use Tie::Hash; package Hash::Add; @Hash::Add::ISA = qw /Tie::StdHash/; use overload '+' => sub { my ($self, $other) = @_; foreach my $key (keys %$self) { $self -> {$key} += $other -> {$key}; } }; package main; my $totals = tie my %totals => 'Hash::Add'; my $delta = tie my %delta => 'Hash::Add'; my @fields = qw /foo bar/; @totals {@fields} = (10, 20); @delta {@fields} = (5, 6); $totals += $delta; print "Foo = $totals{foo}\n"; print "Bar = $totals{bar}\n"; __END__ Foo = 15 Bar = 26
-- Abigail
In reply to Re: Using += in array context
by Abigail
in thread Using += in array context
by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |